
Python Date Time can be used in Python by importing DateTime Modules. We can also work with Calendar using the Calendar Module.
Importing Date Time and Calendar Modules
import datetime
import calendar
Date function in python
Once we have imported modules then we can work on a date, time and calendar in Python
import datetime
x = datetime.datetime.now()
print(x)
If you want specific Year, Month, Day then
x = datetime.datetime.now()
print(x)
print(x.year)
print(x.strftime("%B"))
print(x.strftime("%A"))
Create Date Time Object or Variable
For creating user-defined DateTime object use DateTime module name. For Example –
y = datetime.datetime(2020,8,19)
print(y)
Python Calendar
We can work with the calendar using the calendar module. For printing a specific month of the year. For Example:
cal = calendar.month(2017,2)
print(cal)
Whats Next – Python Function