Python - datetime Module

Python

Share
python logo

In this tutorial we will learn about datatime module in Python.

We learned about modules in the Python - Modules tutorial. Feel free to check that out.

Importing the datetime module

To work with data and time in Python we can take help of the datetime module.

To import the module we write the following.

import datetime

Display the current date and time

We can write the following Python code to display the current date and time using the datetime module.

# import module
import datetime

# current date time
currDT = datetime.datetime.now()

# output
print(currDT)

The above Python code will give us the following output.

2018-10-21 04:43:58.846551

The date time is in the following format.

Year-Month-Day Hour:Minute:Second.Microsecond

Create date object using datetime() method

In the following Python program we are creating date object for the date 1st Jan 2000 using the datetime module.

# import module
import datetime

# create date
date = datetime.datetime(2000, 1, 1)

# output
print(date)

The above code will print the following output.

2000-01-01 00:00:00

In the following Python program we are creating a date object for the date time 1st Jan 2000 10:20:30.

# import module
import datetime

# create date time
date = datetime.datetime(2000, 1, 1, 10, 20, 30)

# output
print(date)

The above code will give the following output.

2000-01-01 10:20:30

The strftime method

We use the strftime() method to convert the date object into human readable string.

The strftime method takes one argument, the format.

In the following Python program we are printing the current date in human readable format using the strftime method.

# import module
import datetime

# current time
currDT = datetime.datetime.now()

# output
print("Day", currDT.strftime('%a'))
print("Date", currDT.strftime('%d'))
print("Month", currDT.strftime('%b'))
print("Year", currDT.strftime('%Y'))
print("Hour", currDT.strftime('%H'))
print("Min", currDT.strftime('%M'))
print("Sec", currDT.strftime('%S'))

The above code will give us a similar output.

Day Sun
Date 21
Month Oct
Year 2018
Hour 07
Min 34
Sec 58

Format for the strftime method

Format Description Example
%a Name of the weekday in short form. Sun
%A Full name of the weekday. Sunday
%w Weekday in number form 0-6.
Sunday = 0
Monday = 1
...
Saturday = 6
0
%d Day of the month from 01-31. 01
%b Name of the month in short form. Oct
%B Full name of the month. October
%m Month in number form 01-12.
January = 01
February = 02
...
December = 12
10
%y Short form year without century. 20
%Y Full year. 2020
%H Hour in 24-hour format 00-23. 20
%I Hour in 12-hour format 00-12. 10
%M Minute 00-59. 20
%S Second 00-59. 30
%f Microsecond 000000-999999. 102030
%p This represents the AM/PM. AM
%z UTC offset +0530
%Z Timezone IST
%j Day number of year 001-366.
1st Jan = 001
31st Dec = 365
For leap year, 31st Dec = 366
294
%U Week number of year 00-53.
Sunday as the first day of the week.
42
%W Week number of year 00-53.
Monday as the first day of the week.
42
%c Locale date and time. Sat Jan 1 10:20:30 2000
%x Locale date. 01/01/00
%X Locale time. 10:20:30
%% The % character. %
Share