How to get current date in python?
How to get current date in python?
In Python programming language, date and time are not just a data form, but it is possible to import a method called datetime to operate with both the date and time. The Datetime element is integrated with Python, so it does not require to be installed individually.
The datetime module offers some operations for obtaining both the current date and time. Let's try them out.
date.today() method
today() method of date class lies inside the module of datetime. This method is used to give a date object that give us the value of today’s date. It returns the current local date.
Syntax
Here, we displayed the syntax of above method.
date.today()
Example
// Python program to get current date from datetime import date //Importing date class from datetime module today = date.today() print("current local date is:", today) // Returns the current local date
Output
After the successful execution of the above program code, we got the following output.
datetime.now()
Python Library describes the ability that camwood should be used primarily to get the existing opportunity when it's also a date. Now() method takes advantage of the current date and time of the neighborhood, which can be specified in the datetime module. This method returns the existing time and date in time format.
Syntax
Here, we have displayed the syntax of above method.
datetime.now(tz)
Parameters
tz: It will be used to specify the time zone of the existing possibility; an additional date may be required. (Uses Greenwich meridian run through Toward default).
from datetime import datetime // Importing datetime class from datetime module X = datetime.now() // returns current date and time Print = (“X =”, X)
Output
We got the following output, after successful execution of the above program code.
Attributes of now() function
The now() method contains several attributes including time such as year, month, hour, minute, second.
Example
Here, an example code is given below to display the attributes of now().
// attributes of now() import datetime // importing datetime module for now() current_time = datetime.datetime.now() // utilizing now() to get the current time print ("The attributes of now() are displayed below : ") // Printing attributes of now() print ("Year : ", end = "") print (current_time.year) print ("Month : ", end = "") print (current_time.month) print ("Day : ", end = "") print (current_time.day)
Output
After the successful execution of the above code, we got the below given output.
Strftime() method
The datetime entity contains a procedure for syntax date artifacts into comprehensible strings.
The method is considered as strftime(), and requires only single parameter, form, to clarify the template of the retrieved string:
Example:1
Here, we have example to show the name of the month.
Import datetime // Importing datetime module for Strftime() method a = datetime.datetime (2021, 02, 24) print = (a.strftime(“%B”)) // Printing the name of the month
Output
After the execution of the above program code, we got the below displayed output.
Example: 2
Here, we have example to show the year and the name of weekday.
import datetime // importing datetime module for strftime() method y = datetime.datetime.now() print(y.year) print(y.strftime(“%A”)) // printing year and name of the weekday
Output
We got the following output, after the successful execution of the above program code.