Adding a date or time to your program is really easy with Python. All you have to do is import date or import time and then use the available method calls.
Most likely though, you are here because you need something more than that. You probably need to convert Epoch seconds to a date format. You may want to know how to get the differences between the two dates.
There are many ways to manipulate the date and time modules in Python. At the same time, it also means the way to get the date or time you want may not be straightforward.
With this guide at your disposal, it won’t be too difficult. Use it as a reference whenever you are stuck with a date or time you are trying to convert from one format to another.
The three modules you want to be familiar with when manipulating the date and time are shown below. Depending on your needs, you will use one or more of them. In the examples to follow, I will not import these modules anymore since they should be the first thing you do.
This is an example of using the time module to get the day of the year.
print “Today is day”, time.localtime(), “of the current year.”
Output: Today is day 72 of the current year.
This is an example of using the datetime module to display the day and year.
today = datetime.date.today()
print “Today is day”, today.timetuple(), “of”, today.year
Output: Today is day 72 of 2011
This is an example of converting the day into a string format.
print “Today is day”, today.strftime(“%j”), “of the current year.”
Output: Today is day 072 of the current year.
Convert Date Format to Epoch Seconds
Suppose you want to convert a date given in a tuple date format to Epoch seconds. The way to do that is by using either the datetime or calendar module.
When given a local time in tuple format, convert it to Epoch seconds.
today = datetime.datetime.now()
print “Epoch seconds:”, time.mktime(today.timetuple())
Output: Epoch seconds: 1300055526.0
When given a UTC time in tuple format, convert it to Epoch seconds.
today = datetime.datetime.utcnow()
print “Epoch seconds:”, calendar.timegm(today.timetuple())
Output: Epoch seconds: 1299974400
Convert Epoch Seconds to Date Format
If you want to reverse the roles and convert the other way around, you can use either the datetime or time module.
For the local time in Epoch seconds
today = datetime.datetime.fromtimestamp(1300055526.0)
Output: 2011-03-13 15:32:06
today = datetime.datetime.utcfromtimestamp(1300055526.0)
Output: 2011-03-13 22:32:06
Using the time module
today = time.localtime(1300055526.0)
Output: time.struct_time(tm_year=2011, tm_mon=3, tm_mday=13, tm_hour=15, tm_min=32, tm_sec=6, tm_wday=6, tm_yday=72, tm_isdst=1)
Convert Datetime to String
If you work with datetime, you may find that sometimes you don’t want to leave it in a date format tuple but convert it to a datetime string such as MM/DD/YYYY. The way to do it is by using the strftime method.
For the local time in epoch seconds
today = datetime.datetime.today()
Output: 2011-03-26 21:19:15.937828
print “Today is”, today.strftime(“%m/%d/%Y”)
If you also want to include the hour, minute, and seconds, use the following format instead:
print “Today is”, today.strftime(“%x %X”)
The output of the command: ’03/26/11 21:19:15′
Convert String to Datetime
If you have a string MM/DD/YYYY and you want to convert it to a date, you will have to match the string with the date format.
Suppose you want to convert a string with datetime ‘2011-03-26 21:19:15.937828’
date_string = ‘2011-03-26 21:19:15.937828’
date_format = ‘%Y-%m-%d %H:%M:%S.%f’
time_tuple = time.strptime(date_string, date_format)
Output: time.struct_time(tm_year=2011, tm_mon=3, tm_mday=26, tm_hour=21, tm_min=19, tm_sec=15, tm_wday=5, tm_yday=85, tm_isdst=-1)
If you prefer the date format ‘2011-03-26 21:19:15’ but recognized as a datetime object, you can convert it using datetime.
Output: 2011-03-26 21:19:15.000005
Adding to or Subtracting from a Date
To add to or subtract from a date, use the timedelta method in the datetime module. You start with a datetime and then use the timedelta to add or delete the difference in time.
now = datetime.date(2011, 3, 13)
difference1 = datetime.timedelta(days=-1)
difference2 = datetime.timedelta(weeks=3)
print “One day in the past is:”, now + difference1
Output: One day in the past is: 2011-03-12
print “Three weeks in the future is:”, now + difference2
The output of the command: Three weeks in the future is: 2011-04-03
print datetime.date(2011, 3, 13) – datetime.date(2010, 3, 13)
Output: 365 days, 0:00:00