How to calculate the days between two dates using Python. Quickly.

by Stii

This is one of those pretty useless things, but you never know when you might just have the need for something like this… I have not taken the time to investigate a quicker way using Bash, but my love of Python made me use it without even thinking twice! :)

I first fired up the interactive Python interpreter by simply typing the command python. Next, I imported the datetime module. Did a simple timedelta between two datetime objects and Jack’s your uncle.

$ python
Python 2.5.1 (r251:54863, Apr 15 2008, 22:57:26)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license"
for more information.
>>> import datetime
>>> print (datetime.date(2009, 03, 31) \
... - datetime.date(2009, 02, 06)).days
53
>>>

Python not only is an excellent programming language, it is also a brilliant general purpose toolbox! Bash/Awk/whatever experts, is there a quicker way maybe? Would love to know.