Recipe 3.1. Calculating Yesterday and Tomorrow
Credit: Andrea
Cavalcanti
Problem
You want to get today''s date, then calculate
yesterday''s or tomorrow''s.
Solution
Whenever you have to deal with a
"change" or
"difference" in time, think
timedelta:
import datetime
today = datetime.date.today( )
yesterday = today - datetime.timedelta(days=1)
tomorrow = today + datetime.timedelta(days=1)
print yesterday, today, tomorrow
#emits: 2004-11-17 2004-11-18 2004-11-19
Discussion
This recipe''s Problem has
been a fairly frequent question on Python mailing lists since the
datetime module arrived. When first confronted
with this task, it''s quite common for people to try
to code it as yesterday = today - 1, which gives a
TypeError: unsupported operand type(s) for -:
''datetime.date'' and ''int''.Some people have called this a bug, implying that Python should guess
what they mean. However, one of the guiding principles that gives
Python its simplicity and power is: "in the face of
ambiguity, refuse the temptation to guess." Trying
to guess would clutter datetime with heuristics
meant to guess that you "really meant 1
day", rather than 1 second (which
timedelta also supports), or 1 year.Rather than trying to guess what you mean,
Python, as usual, expects you to make your meaning explicit. If you
want to subtract a time difference of one day, you code that
explicitly. If, instead, you want to add a time difference of one
second, you can use timedelta with a
datetime.datetime object, and then you code the
operation using exactly the same syntax. This way, for each task you
might want to perform, there''s only one obvious way
of doing it. This approach also allows a fair amount of flexibility,
without added complexity. Consider the following interactive snippet:
>>> anniversary = today + datetime.timedelta(days=365)# add 1 yearKeep in mind that, if you want fancier control over date and time
>>> print anniversary
2005-11-18
>>> t = datetime.datetime.today( ) # get right now
>>> t
datetime.datetime(2004, 11, 19, 10, 12, 43, 801000)
>>> t2 = t + datetime.timedelta(seconds=1) # add 1 second
>>> t2
datetime.datetime(2004, 11, 19, 10, 12, 44, 801000)
>>> t3 = t + datetime.timedelta(seconds=3600) # add 1 hour
>>> t3
datetime.datetime(2004, 11, 19, 11, 12, 43, 801000)
arithmetic, third-party packages, such as dateutil
(which works together with the built-in datetime)
and the classic mx.DateTime, are available. For
example:
from dateutil import relativedeltaHowever, "always do the simplest thing that can
nextweek = today + relativedelta.relativedelta(weeks=1)
print nextweek
#emits: 2004-11-25
possibly work." For simple, straightforward tasks
such as the ones in this recipe,
datetime.timedelta works just fine.
See Also
dateutil documentation at DateUtil, and
datetime documentation in the Library
Reference. mx.DateTime can be found at
http://www.egenix.com//image/library/english/10241_python/mxDateTimel.
mx.DateTime can be found at http://www.egenix.com//image/library/english/10241_python/mxDateTimel.