Python datetime Module Tutorial

Python provides the datetime module in order to work with the date and time information. The datetime module provides a useful date and time-related date types and methods for different operations. In this tutorial, we will learn how to use the datetime module data types and methods.

Import datetime Module

In order to use the date object, timestamp etc. the datetime module should be imported with the import statement like below.

import datetime

List datetime Module Data Types, Methods

As datetime module provides different date and time related data types, methods etc. they can be listed with the dir() method like below.

dir(datetime)
['MAXYEAR', 'MINYEAR', 'builtins', 'cached', 'doc', 'file', 'loader', 'name', 'package', 'spec', 'date', 'datetime', 'datetime_CAPI', 'sys', 'time', 'timedelta', 'timezone', 'tzinfo']

Get Current Date and Time with now() Method

The current date and time can be get with the now() method provided by datetime. In the following example we will print the current date and time.

import datetime

now = datetime.datetime.now()

print(now)

The output is like below in YYYY-MM-DD HH:MM:SS.SSSSSS format where miliseconds are printed too.

2021-01-14 06:27:47.516828

Create Date Object with Specified Date and Time

The datetime object of the datetime module can be used to create a specific date with the provided values. All information about the date time like the year, month, day, hour, minute, second can be provided explicitly. The datetime() method is used to create a datetime object according to the specified date and time values.

import datetime

#Specify year, month and day for date
a = datetime.datetime(2021,1,13)

#Specify year, month, day, hour, minute, second for date and time
b = datetime.datetime(2021,1,13,12,20,55)

Format Date and Time Output with strftime() Method

While working with date and time information it is important to express or format the date and time information in different formats. For example, the month can be expressed with the month name like January, etc. The strftime() method can be used to format date and time information which is stored in a datetime object.

import datetime

#Specify year, month, day, hour, minute, second for date and time
x = datetime.datetime(2021,1,13,12,20,55)

#Weekday, short version	Wed
print(x.strftime("%a"))

#Weekday, full version	Wednesday
print(x.strftime("%A"))

#Weekday as a number 0-6, 0 is Sunday	3
print(x.strftime("%w"))

#Day of month 01-31	31
print(x.strftime("%d"))

#Month name, short version	Dec
print(x.strftime("%b"))

#Month name, full version	December
print(x.strftime("%B"))

#Month as a number 01-12	12
print(x.strftime("%m"))

#Year, short version, without century	18
print(x.strftime("%y"))

#Year, full version	2018
print(x.strftime("%Y"))

#Hour 00-23	17
print(x.strftime("%H"))

#Hour 00-12	05
print(x.strftime("%I"))

#AM/PM	PM
print(x.strftime("%p"))

#Minute 00-59	41
print(x.strftime("%M"))

#Second 00-59	08
print(x.strftime("%S"))

#Microsecond 000000-999999	548513
print(x.strftime("%f"))

#UTC offset	+0100
print(x.strftime("%z"))

#Timezone	CST
print(x.strftime("%Z"))

#Day number of year 001-366	365
print(x.strftime("%j"))

#Week number of year, Sunday as the first day of week, 00-53	52
print(x.strftime("%U"))

#Week number of year, Monday as the first day of week, 00-53	52
print(x.strftime("%W"))

#Local version of date and time	Mon Dec 31 17:41:00 2018
print(x.strftime("%c"))

#Local version of date	12/31/18
print(x.strftime("%x"))

#Local version of time	17:41:00
print(x.strftime("%X"))

#A % character	%
print(x.strftime("%%"))

#ISO 8601 year	2018
print(x.strftime("%G"))

#ISO 8601 weekday (1-7)	1
print(x.strftime("%u"))

#ISO 8601 weeknumber (01-53)	01
print(x.strftime("%V"))

Get Date From A TimeStamp

The timestamp is a date and time representation popularly used in Unix, Linux, and BSD systems. the timestamp expresses the total second count from January 1, 1970. In other words total second from January 1, 1970, 00:00. The timestamp used in Linux systems for logs and other operations.

import datetime

x= datetime.date.fromtimestamp(1529294364)

print(x)
2018-06-18

timedelta Difference Between Two Dates or Time

The timedelta is another useful type provided by the datetime module. The timedelta is used to express the difference between two dates and times. The timedelta stores the difference as the year, month, day, hour, minute, and second. The timedelta between two dates and time object can be calculated like below.

import datetime

a = datetime.datetime(2021,1,13,12,20,55)

b = datetime.datetime(2018,5,23,14,40,15)

td = a -b

print(td)

The output contains the total days with hours, minutes and secods information.

965 days, 21:40:40

Leave a Comment