Sunday, June 14, 2020

Python Date and time

Python Date and Time

The datetime module

The date and time are not data types in Python, rather they are provided by the Python's datetime module. The datetime module offers classes for manipulating dates and times. The date and time objects may be categorized as “aware” or “naive” depending on whether or not they include timezone information or not.

We can see a simple example, before moving ahead in more detail.

import datetime

dtnow =

2020-06-14 12:15:40.392774

The now() method is called on the class datetime in the datetime module. The now() method returns object containing the current local date and time.

We can further extract the individual information fro this object.

import datetime
dtnow =
#Further Extracting 


The method today() applied on the date class in datetime module to return today's local date.

import datetime

dttoday =


What is inside the datetime module?

To see, what is contained by the datetime module, we can use the dir() method.

import datetime


['MAXYEAR', 'MINYEAR', '__builtins__', '__cached__', '__doc__',
'__file__', '__loader__', '__name__', '__package__', '__spec__', 
'date', 'datetime', 'datetime_CAPI',
'time', 'timedelta', 'timezone', 'tzinfo']

Constants in datetime module 

This module provides the following constants,

  • datetime.MINYEAR represents the smallest year number allowed in a date or datetime object. MINYEAR is 1.
  • datetime.MAXYEAR represents the largest year number allowed in a date or datetime object. MAXYEAR is 9999.

Classes in datetime module

The types offered by the datetime module


Represents a naive date, considering the current Gregorian calendar always was, and always will be, in effect. The class has year, month, and day as attributes.

class datetime.time

Represents idealized time, considering that every day has exactly 24*60*60 seconds. ( no “leap seconds” notion). This class has hour, minute, second, microsecond, and tzinfo as attributes.

class datetime.datetime

This class represents a combination of a date and a time. The class has year, month, day, hour, minute, second, microsecond, and tzinfo as attributes.

class datetime.timedelta

A duration representing the difference between two date, time, or datetime instances to in microseconds.

class datetime.tzinfo

This is an abstract base class for time zone information objects. These are used by the datetime and time classes to provide a customizable notion of time adjustment (for example, to account for time zone and/or daylight saving time).

class datetime.timezone

A class that implements the tzinfo abstract base class as a fixed offset from the UTC.

Creating Date Objects 

We can create a date object, use the datetime() class (constructor) of the datetime module. The datetime() constructor needs three arguments to create a date, year, month, and day. For example,

import datetime

d = datetime.datetime(2020, 6, 14)


2020-06-14 00:00:00

strftime() Method 

The datetime objects can be formatted into readable component strings with strftime() method. The method takes one parameter, format, to provide the format of the received string. For example, the month form the datetime object can be extracted as,

import datetime

d = datetime.datetime(2020, 6, 11)



Few format string for this method are,

%a  Weekday, short version Wed
Weekday, full version Wednesday
%w  Weekday as a number 0-6, 0 is Sunday 3
%d  Day of month 01-31 31
%b  Month name, short version Dec
%B  Month name, full version December
Month as a number 01-12 12
%y  Year, short version, without century 18
%Y  Year, full version 2018 %H Hour 00-23 17

Get date from a timestamp 

The date object can also be created using the timestamp. A timestamp is the number of seconds between a particular date and January 1, 1970 at UTC. A timestamp can be converted into a date using fromtimestamp() method.

For example,

import datetime

ts =
print("Date =", ts)

Date = 2024-09-13

import datetime

ts = datetime.datetime.fromtimestamp(1726244364)
print("Date =", ts)

Date = 2024-09-13 16:19:24

Getting today's year, month and day 

The year, month, day, day of the week etc. can be easily extracted from the date object easily. For example,

import datetime

ts = datetime.datetime.fromtimestamp(1626244364)
print("year:", ts.year)
print("month:", ts.month)
print("hour =", ts.hour)
print("minute =", ts.minute)
print("second =", ts.second)

year: 2021
month: 7
day: 14
hour = 6
minute = 32
second = 44


The timedelta object can be used to get the difference between two dates or times. For example,

from datetime import datetime, date

t1 = date(year = 2020, month = 7, day = 10)
t2 = date(year = 2016, month = 11, day = 21)
t3 = t1 - t2
print("t3 =", t3)

t4 = datetime(year = 2017, month = 7, day = 12, hour = 7, minute = 9, second = 33)
t5 = datetime(year = 2019, month = 6, day = 10, hour = 5, minute = 55, second = 13)
t6 = t4 - t5
print("t6 =", t6)

print("type of t3 =", type(t3)) 
print("type of t6 =", type(t6)) 


t3 = 1327 days, 0:00:00
t6 = -698 days, 1:14:20
type of t3 = <class 'datetime.timedelta'>
type of t6 = <class 'datetime.timedelta'>