Python datetime example

Python has the datetime module to help deal with timestamps in your code. Time values are represented with the time class. Times have attributes for hour, minute, second, and microsecond. They can also include time zone information. The arguments to initialize a time instance are optional, but the default of 0 is unlikely to be what you want.


Lets take a look at how we can extract time information from the datetime module. We can create a time-stamp by specifying datetime.time(hour,minute,second,microsecond)

import datetime t = datetime.time(6, 25, 1) print(t) print('hour :', t.hour) print ('minute:', t.minute) print ('second:', t.second) print ('microsecond:', t.microsecond) print ('tzinfo:', t.tzinfo)


hour : 6
minute: 25
second: 1
microsecond: 0
tzinfo: None

Time does not deal with date at all, it just stores time information.

We can also check min and max values of time in the day.

print 'Earliest :', datetime.time.min print 'Latest :', datetime.time.max print 'Resolution:', datetime.time.resolution


Earliest : 00:00:00
Latest : 23:59:59.999999
Resolution: 0:00:00.000001

The min and max class attributes denotes the valid range of times in a single day.


datetime deals with date timestamps too. Calendar date values are represented with the date class.
Date Instances have attributes for year, month, and day. You can create a date representing today’s date using the today() class method.

Lets see some examples:

today = print(today) print ('ctime:', today.ctime()) print ('tuple:', today.timetuple()) print ('ordinal:', today.toordinal()) print ('Year:', today.year) print ('Mon :', today.month) print ('Day :',


ctime: Mon Nov 27 00:00:00 2017
tuple: time.struct_time(tm_year=2017, tm_mon=11, tm_mday=27, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=331, tm_isdst=-1)
ordinal: 736660
Year: 2017
Mon : 11
Day : 27

As with time, the range of date values supported can be determined using the min and max attributes.

print('Earliest :', print ('Latest :', print ('Resolution:',


Earliest : 0001-01-01
Latest : 9999-12-31
Resolution: 1 day, 0:00:00

You can create date using method and can use replace method to replace year,month etc. of date object
For example:
You can change year using replace function as below.

date1 =, 11, 27) print('date1:', date1) date2 = date1.replace(year=1999) print 'date2:', date2


date1: 2017-11-27
date2: 1999-11-27


We can performa various arithmetic operation such as diff, add on date object.Let’s see with the help of some example.

from datetime import timedelta date1 =, 11, 27) print('date1:', date1) # Add one day to date 1 print('date1 + 1 day:', date1+ timedelta(days=1)) date2 = date1.replace(year=1999) print('date2:', date2) #Different between two dates print(date1-date2)


date1: 2017-11-27
date1 + 1 day: 2017-11-28
date2: 1999-11-27
6575 days, 0:00:00

This give us the difference in days between the two dates.
As You can see we can use the timedelta method to specify various units of times (day,minutes,hours,etc…)
That’s all about datetime module in python

Leave a Reply

Your email address will not be published. Required fields are marked *