- Python Date Time Operations Tutorial with Examples
- What is Time?
- Create Time Object With Current Time
- Time Object Minimum and Maximum Values
- Create Time Object Providing Values
- Date Operations
- Date Object Minimum and Maximum Values
- Copy Date Instance With New Values
- Time Delta Or Time Difference
- Date Arithmetic
- Comparing Date and Time
- Date and Time Formatting
Python Date Time Operations Tutorial with Examples
Python datetime library provides a lot of different functionalities to operate with date and time values. Python datetime library can be found later versions from 2.3. There are datetime library-related libraries like time and calendar if you are interested in a specific issue.
What is Time?
Time values are represented with time class. Time class gives the ability to hold values like an hour, minute, second, microsecond. Time values also hold time zone information.
Create Time Object With Current Time
We will create a new time object with the current time. Time objects will hold values like hour, minute, second, time zone values.
from datetime import datetime, date, time t = datetime.now() print t print 'hour :', t.hour print 'minute:', t.minute print 'second:', t.second print 'microsecond:', t.microsecond print 'tzinfo:', t.tzinfo
Time Object Minimum and Maximum Values
Time object has limits to express values. Below maximum and minimum values are printed by using min and max properties.
import datetime print 'Min :', datetime.time.min print 'Max :', datetime.time.max print 'Resolution:', datetime.time.resolution
Create Time Object Providing Values
A new time object can be created by providing minute, second and other related values with parameters.
import datetime t = datetime.time(3, 2, 1) print t print 'hour :', t.hour print 'minute:', t.minute print 'second:', t.second print 'microsecond:', t.microsecond print 'tzinfo:', t.tzinfo
Date Operations
Date class provides to hold information about the year, month, day. today function can be used to initialize new date object like below.
import datetime today = datetime.date.today() print today print 'tuple :', today.timetuple() print 'ordinal:', today.toordinal() print 'Year :', today.year print 'Month :', today.month print 'Day :', today.day
In this example, we print date-time information as a tuple with timetuple function.
Date Object Minimum and Maximum Values
Here are date object minimum and maximum values.
import datetime print 'Minimum :', datetime.date.min print 'Maximum :', datetime.date.max print 'Resolution :', datetime.date.resolution
Copy Date Instance With New Values
Existing date instance or object can be copied to the new date object by using the replace() function. Replace function will replace if a property and value are given below.
import datetime d1 = datetime.date(2015, 1, 1) print 'date1:', d1 d2 = d1.replace(year=2016) print 'date2:', d2
Time Delta Or Time Difference
Time delta can be used to get the difference between two date and time objects. We define two date object 1.1.2015 and 1.1.2016. Then we get time delta of two date objects and print it.
import datetime d1 = datetime.date(2015, 1, 1) print 'date1:', d1 d2 = d1.replace(2016, 1, 1) print 'date2:', d2 td=d2-d1 print 'Difference:'+str(td)
Date Arithmetic
Arithmetic operators can be used to operate on date and time objects. We can sum these time objects like below.
import datetime today = datetime.date.today() print 'Today :', today two_day = datetime.timedelta(days=2) print 'Two day :', two_day before_yesterday = today - two_day print 'Before Yesterday:', before_yesterday after_tomorrow = today + two_day print 'After Tomorrow :', after_tomorrow print 'tomorrow - yesterday:', after_tomorrow - before_yesterday print 'yesterday - tomorrow:', before_yesterday - after_tomorrow
Comparing Date and Time
Comparing date and time objects are similar to primitive data types like integer and string. We can compare two objects with regular compare keywords and operators.
import datetime import time print 'Times:' t1 = datetime.time(11, 55, 0) print '\tt1:', t1 t2 = datetime.time(12, 5, 0) print '\tt2:', t2 print '\tt1 < t2:', t1 < t2 print 'Dates:' d1 = datetime.date.today() print '\td1:', d1 d2 = datetime.date.today() - datetime.timedelta(days=1) print '\td2:', d2 print '\td1 >d2:', d1 > d2
Date and Time Formatting
While printing to the stdout or other direction date and time formats can be formatted with strftime() function like below.
import datetime format = "%b %a %d %H:%M:%S %Y" today = datetime.datetime.today() print 'ISO :', today s = today.strftime(format) print 'strftime:', s d = datetime.datetime.strptime(s, format) print 'strptime:', d.strftime(format)