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.

Example

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.

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).

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().

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.

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.

Output

We got the following output, after the successful execution of the above program code.

Pin It on Pinterest

Share This