9.1 timedelta basics

First import the timedelta class from the datetime module

These first three examples of methods available from the time deltaclass are straight-forward.

from datetime import date
from datetime import time
from datetime import datetime
from datetime import timedelta
 
def main():
 
  # construct a basic timedelta and print it
  print timedelta(days=365, hours=5, minutes=1)
 
  # print today's date
  print "today is: " + str(datetime.now())
 
  # print today's date one year from now
  print "one year from now it will be: " + str(datetime.now() + timedelta(days=365))
 
if __name__ == "__main__":
  main()

Note you can add together two dates...

9.2 timedelta and arguments

In this next case the timedelta method is taking in two arguments both of which have default values set.

from datetime import date
from datetime import time
from datetime import datetime
from datetime import timedelta
 
def main():
 
  # create a timedelta that uses more than one argument
  print "in two weeks and 3 days it will be: " + str(datetime.now() + timedelta(weeks=2, days=3))
 
if __name__ == "__main__":
  main()

9.3 calculate the date 1 week ago, formatted as a string

Here we use the time formatting method .strftime() to format the output string from the calculation.

from datetime import date
from datetime import time
from datetime import datetime
from datetime import timedelta
 
def main():
 
  # calculate the date 1 week ago, formatted as a string
  t = datetime.now() - timedelta(weeks=1)
  s = t.strftime("%A %B %d, %Y")
  print "one week ago it was " + s
 
if __name__ == "__main__":
  main()

9.4 How many days until April Fools' Day?

from datetime import date
from datetime import time
from datetime import datetime
from datetime import timedelta
 
def main():
 
### How many days until April Fools' Day?
 
  today = date.today()  # get today's date
  afd = date(today.year, 4, 1)  # get April Fool's for the same year
  # use date comparison to see if April Fool's has already gone for this year
  # if it has, use the replace() function to get the date for next year
  if afd < today:
    print "April Fool's day already went by %d days ago" % ((today-afd).days)
    afd = afd.replace(year=today.year + 1)  # if so, get the date for next year
 
  # Now calculate the amount of time until April Fool's Day  
  time_to_afd = abs(afd - today)
  print time_to_afd.days, "days until next April Fools' Day!"
 
if __name__ == "__main__":
  main()

Since April Fools Day occurs on the same day every year it can be retrieved for any given year with date(today.year, 4, 1)

Note the use of the control code in the print text followed by the logic to populate the control code %d days ago" % ((today-afd).days)

April Fools Days is first defined in the variable afd. To conditionally swap the value of the variable call the afd.replace(year=today.year + 1) method on the variable. So the value of the variable is only replaced if the condition is met.

Note the use of the absolute value math function to make sure the reuslt of the subtraction is a positive number abs(afd - today)

Lastly we call the .days method on the result to get back days.