Python: How to Get the Current Date?
Here, we’ll learn how to get the current date in Python in a number of different ways. An app, website, or database server’s date is crucial since it provides information about the software’s creation date, the time and date of website entries kept on the server, the app’s version number, and so on. When a user performs a task in one of the supported programmes or on the supported website, the software logs the user’s activity along with the time and date. Additionally, we can claim that every social media site, including Twitter, Facebook, WhatsApp, etc., stores the user’s information, including the date of the user’s post, share, or send the message, etc. Now we may specify how the programme treats the time and date.
In the
Language for Programming in Python
It’s possible to get at the system’s current date and time using a datetime class, which can be found in the datetime module.
Using the DateTimeNow() Technique
The current time and date may be seen using the Python datetime.now() function. Purpose inside the system is defined.
datetime
the Python library module.
Syntax:
datetime.now()
Take, for example, a software that queries the datetime for the current system time.
Python’s now() function, to be specific.
program.py
import datetime
dt = datetime.datetime.now() # use now() method in datetime
print( "Display the current date of the system: ") # current date
print (str (dt) ) # call the dt variable to print the system date.
Output:
Display the current date of the system: 2021-02-28 18:56:52.799555
When Utilizing the “Now” Technique
Method now():
The system date is shown using the now() function in Python.
Syntax:
now() # fetch the current date
Imagine an application that uses Python’s now() function to get the current system date and time.
Program.py
# import the datetime class or module from the datetime module.
from datetime import datetime
now = datetime.now() # Use now() to access the current date and time
print("Current date and time is ", now)
Output:
Current date and time is 2021-02-28 18:58:33.237779
Importing the datetime module in Python yields a string representing the current system date.
properties of the now() function
Several properties, much to those of time, are available in Python’s now() function.
A time and date stamp with the hours, minutes, and seconds
.
Let’s write some code to illustrate the many options available when printing the date using Python’s now() function.
Program2.py
# importing the datetime module for now() method
import datetime
# Use datetime.now() method to get the current date into the variable current_date
current_date = datetime.datetime.now()
# print the message for now() attributes in Python
print (" Following are the attributes of now() function are : ")
# print the current year of the system
print(" The current year is : ", end = "" )
print (current_date.year)
# print the current month of the system
print( " The current month is : ", end = "" )
print (current_date.month)
# print the current date of the system
print( " The current date is : ", end = "" )
print (current_date.day)
Output:
Following are the attributes of now() function are : The current year is: 2021 The current month is: 2 The current date is: 28
Using Today’s Date Input () Method
Existence of a Date Module Class in the
datetime
module that, when called, will provide a date object with today’s date set in it. In Python, the current date is retrieved through the today() function.
Syntax:
date.today()
Here we’ll think about a programme that uses the today() function of the date class to show the current date.
Prog.py
from datetime import date
td = date.today() # Use the today method and assign it to the td variable.
print(" Get the today date in Python is: ", td)
Output:
Get the today date in Python is: 2021-02-28
To Use the Now().Date() Function
The current system date may be retrieved through the now().date() function by using the DateTime.now() import.
datetime
Python module.
Syntax:
Current_date = datetime.now().date()
Let’s get together and code up a solution to have the system always display the current date.
datetime.now().date()
Pythonic technique.
Program3.py
from datetime import datetime
# Return the current date of the system
Current_date = datetime. now(). date()
print("The current date is :", Current_date)
Output:
The current date is : 2021-02-28
Making Use of the now().time() Method
By importing the time() method from the DateTime class, the now() function may be used to get the current system time.
datetime
Pylon module.
Syntax:
Current_date = datetime.now().time()
Let’s create some code to output the datetime as it is right now on the machine.
now().
Python’s time() function is a useful tool.
Prog2.py
from datetime import datetime
# Return the current date and time of the system
Current_time = datetime.now().time()
print("Current time is :", Current_time)
Output:
Current time is: 19:07:45.787512
Python Date and Time Formats
Here are some examples of how to send arguments to Python’s datetime module to receive the date and time in the format you choose.
strftime () ()
purpose. table
Let’s make a programme in Python that uses the above-mentioned notations.
Prog1.py
import datetime
dt = datetime.datetime.now()
print ("Current date and time is = %s" % dt)
print ("Date and time in ISO Format = %s" % dt.isoformat())
print ("Current year = %s" %dt.year)
print ("Current month is = %s" %dt.month)
print ("Current date (day) = %s" %dt.day )
print ("represent Date in dd/mm/yyyy format = %s / %s/ %s" % (dt.day, dt.month, dt.year))
print ("Current hour is = %s" %dt.hour)
print (" Current minute is = %s" %dt.minute)
print ("Current Second is = %s" %dt.second)
print ("Representation of time in hh: mm: ss format = %s: %s: %s" % (dt.hour, dt.minute, dt.second))
Output:
Current date and time is = 2021-02-28 19:09:03.739466 Date and time in ISO Format = 2021-02-28T19:09:03.739466 Current year = 2021 Current month is = 2 Current date (day) = 28 Represent Date in dd/mm/yyyy format = 28 / 2/ 2021 Current hour is = 19 Current minute is = 9 Current Second is = 3 Representation of time in hh: mm: ss format = 19: 9: 3
How to Use the strftime() Function
Use the strftime() method, and we can make a programme that shows the current time and date.
Strftime.py
import datetime
dt = datetime.datetime.now()
print (dt.strftime ("%Y - %m - %d %H: %M: %S " )) # represents the date YYYY- MM - DD
# HH: MM: SS
print (dt.strftime ("%d / %m / %Y ")) # represents the date in DD/ MM/ YYYY
print (dt.strftime (" %I: %M: %S %p ")) # represents the time in HH: MM: SS AM/ PM
print (dt.strftime ("%a, %b %d, %Y ")) # represents the day, month date and year
Output:
2021 - 02 - 28 19: 12: 06 28 / 02 / 2021 07: 12: 06 PM Sun, Feb 28, 2021