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 datetimes:
>>> 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¶
Create a program somedate.py that takes two dates as arguments and returns a random date between the two given dates.
Example usage:
$ python somedate.py 1970-01-01 1979-12-31
1974-07-20
$ python somedate.py 2099-12-01 2099-12-31
2099-12-19
Age¶
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¶
Make a Python script weekday.py that prints the day of the week for today. It should work like this:
$ python weekday.py
Thursday
Hint
look up the strftime method on the datetime.datetime object.
Bonus: make your weekday.py script work also work for a given date:
$ python weekday.py 2016-01-25
Monday
$ python weekday.py 2016-02-5
Friday
$ python weekday.py 2000-01-01
Saturday
$ python weekday.py 3000-01-01
Wednesday
Fourth Thursday¶
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.
Last Day¶
Make a function that returns the last day of class given a Monday start date and the number of weeks of the class (assuming it ends on a Friday).