Dates and Times

datetime

Python’s datetime module includes lots of date and time utilities.

>>> import datetime
>>> datetime.date.today()
datetime.date(2016, 2, 4)
>>> right_now = datetime.datetime.now()
>>> right_now
datetime.datetime(2016, 2, 4, 10, 30, 0, 106605)
>>> print(right_now)
2016-02-03 10:30:00.106605
>>> right_now.date()
datetime.date(2016, 2, 4)
>>> right_now.date() == datetime.date.today()
True
>>> right_now.time()
datetime.time(10, 30, 0, 106605)
>>> datetime.datetime(2010, 1, 1)
datetime.datetime(2010, 1, 1, 0, 0)

The date and datetime classes can be used for representing dates and times.

Date Arithmetic

We can use the timedelta class to represent a length of time. We can get a timedelta by subtracting dates or datetime objects:

>>> class_length = datetime.date(2016, 2, 5) - datetime.date(2016, 1, 25)
>>> class_length
datetime.timedelta(11)
>>> class_length.days
11
>>> print(class_length)
11 days, 0:00:00

We can also create a timedelta directly:

>>> datetime.timedelta(hours=6)
datetime.timedelta(0, 21600)

We can add a timedelta to a datetime to get a new datetime.

>>> an_hour = datetime.timedelta(hours=1)
>>> an_hour
datetime.timedelta(0, 3600)
>>> print(an_hour)
1:00:00
>>> right_now
datetime.datetime(2016, 2, 4, 10, 30, 0, 106605)
>>> right_now + an_hour
datetime.datetime(2016, 2, 4, 11, 30, 0, 106605)

We can also do math on timedelta objects:

>>> four_hours = an_hour * 4
>>> four_hours
datetime.timedelta(0, 14400)
>>> an_hour_ago = -1 * an_hour
>>> an_hour_ago
datetime.timedelta(-1, 82800)

We can use the type built-in to check the types of the various objects in the datetime module:

>>> type(right_now)
<class 'datetime.datetime'>
>>> type(datetime.date.today())
<class 'datetime.date'>
>>> type(datetime.time())
<class 'datetime.time'>
>>> type(an_hour)
<class 'datetime.timedelta'>

Date Exercises

Random Date

This is the someday.py exercise in the modules directory. Create the file someday.py in the modules sub-directory of the exercises directory. To test it, run python test.py someday.py from your exercises directory.

Create a program someday.py that takes two dates as arguments and returns a random date between the two given dates.

Example usage:

$ python someday.py 1970-01-01 1979-12-31
1974-07-20
$ python someday.py 2099-12-01 2099-12-31
2099-12-19

Fourth Thursday

This is the fourth_thursday exercise in dates.py.

The San Diego Python group meets on the fourth Thursday of every month. Make a function that takes a year and a month (January = 1, December = 12) and returns the fourth Thursday of that month.

>>> fourth_thursday(2015, 8)
datetime.date(2015, 8, 27)
>>> fourth_thursday(2015, 9)
datetime.date(2015, 9, 24)
>>> fourth_thursday(2015, 10)
datetime.date(2015, 10, 22)
>>> fourth_thursday(2016, 1)
datetime.date(2016, 1, 28)
>>> fourth_thursday(2016, 2)
datetime.date(2016, 2, 25)

Hint

Look up the weekday method on datetime objects.

Age

There are no tests for this exercise.

Make a function to calculate how old someone is given their birthdate as a datetime.date object. If today is February 4, 2016, your function should work like this:

>>> from datetime import date
>>> get_age(date(2015, 2, 4))
1
>>> get_age(date(2015, 2, 5))
0
>>> get_age(date(2015, 2, 3))
1
>>> get_age(date(1980, 2, 29))
35

Weekday Module

There are no tests for this exercise.

Make a Python script weekday.py that prints the day of the week for today. It should work like this:

$ python weekday.py
Thursday