Properly dealing with dates can be hard, but they don’t have to be as long as you understand the basics. I have had to deal a lot with dates lately with a few Django applications, and life got a whole lot easier once I figured out the basics with python. In this post we are going talk about getting dates, difference between date and datetime, and how to add and subtract time. Timezones I want to discuss in a blog post on its own.
The datetime module is what houses all of our objects/classes for dealing with dates. You will need to import datetime whenever you want to deal with dates. It is super convenient to use and is built right into python. The key classes are:
- date – Just a date. (Month, Day, Year)
- time – Time independent of day. (Hour, Minute, Second, Microsecond)
- datetime – Combination of date and time. (Month, Day, Year, Hour, Second, Microsecond)
- timedelta – A duration of time used for manipulating dates
- tzinfo – An abstract class for dealing with timezones
NOTE 1: All of these as objects are immutable.
NOTE 2: Objects of type date are naive, meaning they are not aware of a timezone.
Naive and Aware are two states you need to be aware of when dealing with dates. Naive dates just assume there is no timezone, basically. Aware dates you can use to make adjustments as needed. Again we are going to discuss timezone based dates in another post.
date vs datetime
Date just gets you back the actual date, not time. The best/easiset way to get the date is to create a new object and add in the appropriate numbers.
from datetime import date d = date(2013, 12, 22) print(d.year) print(d.month) print(d.day) print(d.strftime("%Y %m %d"))
This will output:
>>> '2013' >>> '12' >>> '22' >>> '2013 12 22'
Datetime is fairly similar as well, and with some of the same code you can do generate a date and time object to work with.
from datetime import datetime d = datetime(2013, 12, 22, 11, 30, 59) print(d.hour) print(d.minute) print(d.second) print(d.strftime("%m/%d/%Y %I:%M:%S"))
This will output:
>>> 11 >>> 30 >>> 59 >>> 2/22/2013 11:30:59
You may have noticed at the end I used the `strftime` function. This is a convenient function to output the date/time in string format. All you need to do is pass it a string with a symbol and the function interprets it and outputs it properly. Strftime.org is the site I use to get the symbols to add since it looks nicer and is easier to remember than the python docs.
The final thing we shall look at is adding and subtracting time. How I used to do it before I actually looked in the docs to find the proper way was manually get the time using hours, minutes, seconds properties and create a new object adding or subtracting appropriately. After getting frustrated I found the `timedelta` object and it is amazing.
Lets add 3 days to a date object.
from datetime import timedelta, date d1 = date(2012, 12, 20) print(d1.strftime("%Y %m %d")) d2 = d1 + timedelta(days=3) print(d2.strftime("%Y %m %d"))
This will output:
>>> '2013 12 20' >>> '2013 12 23'
You can try to experiment from here with dealing with timedelta objects. It kind of makes dealing with dates fun and easy.
Dealing with dates while programming is a science it itself, an annoying one at times. Fortunately, the python devs have solved the problem, at least as far as I have used it. The great thing about doing dates using python date objects is when adding and subtracting days, years, hours, etc you get a more accurate day and month. Subtracting 53 days from the 14th of March can be a bit complicated if you do it by hand, but with python its super simple. So if you aren’t using date objects in python you should definitely start today.