pretty

Tuesday, October 30, 2012

Adding or subtracting a date in python

Some examples for calculating dates in the past and future.

import datetime
"""
use timedelta for rolling dates
datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0,
hours=0, weeks=0)
"""
a_week_ago = datetime.datetime.now() - datetime.timedelta(weeks=1)
tomorrow = datetime.date.today() + datetime.timedelta(days=1)
print a_week_ago
#2012-10-23 21:58:00.109116
print tomorrow
#2012-10-31
#and so on . . .
more here
and here

No comments:

Post a Comment