python


Python Date and Time

A date in Python is not a data type of its own, but we can import a module named datetime to work with dates as date objects.
Example
Import the datetime module and display the current date:


import datetime

x = datetime.datetime.now()

print(x)
Output:

2020-08-01 20:16:20.989744

When we execute the code from the example above the result will be:
2020-08-01 20:16:20.989744
The date contains year, month, day, hour, minute, second, and microsecond.

Example
Return the year and name of weekday:


import datetime

x = datetime.datetime.now()

print(x.year)
print(x.strftime("%A"))
Output:

2020
Saturday

Creating Date Objects

To create a date, we can use the datetime() class (constructor) of the datetime module.
The datetime() class requires three parameters to create a date: year, month, day.
Example
Create a date object:


import datetime

x = datetime.datetime(2020, 5, 17)

print(x)
Output:


2020-05-17 00:00:00

The datetime() class also takes parameters for time and timezone (hour, minute, second, microsecond, tzone), but they are optional, and has a default value of 0, (None for timezone).