Python – Date Time

Python Date Time can be used in Python by importing Datetime Modules. We can also work with Calendar using Calendar Module.

Importing Date Time and Calendar Modules

import datetime
import calendar

Python DateTime

Once we have imported modules then we can work on 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 DateTime 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 calendar using calendar module. For printing specific month of the year. For Example:

cal = calendar.month(2017,2)
print(cal)
Filed under: Python

No comment yet, add your voice below!


Leave a Reply