Classes date and datetime

The strftime function and its % formats

The Python function strftime in line 17 of date1.py is implemented by calling the C function strftime. On macOS, this C function allows a minus sign between the % and the d in line 18. See “Do not do any padding” in the strftime documentation. On Microsoft, see “Remove leading zeros” here.

Chronological comparison

A < sign between two numbers tells you if the left number is smaller:

a = 10
b = 20

if a < b:
    print(f"{a} is less than {b}.")
10 is less than 20.

A < sign between two strings tells you if the left string comes earlier in alphabetical order:

a = "goodbye"
b = "hello"

if a < b:
    print(f'"{a}" comes before "{b}" in alphabetical order.')
"goodbye" comes before "hello" in alphabetical order.

A < sign between two objects of class datetime.date (line 39 of date1.py), or between two objects of class datetime.datetime, tells you if the left object comes earlier in chronological order.

import datetime

a = datetime.date(2019, 12, 31)
b = datetime.date(2020,  1,  1)

if a < b:
    print(f"{a} is earlier than {b}.")
2019-12-31 is earlier than 2020-01-01.
import datetime

a = datetime.datetime(2019, 12, 31, 23, 59, 59)
b = datetime.datetime(2020,  1,  1,  0,  0,  0)

if a < b:
    print(f"{a} is earlier than {b}.")
2019-12-31 23:59:59 is earlier than 2020-01-01 00:00:00.

The Python script

There’s a lot more stuff you can put into a datetime object: microsecond, fold, tzinfo.

date1.py

2019-12-31
2019-12-31
2019-12-31
Tuesday, December 31, 2019 (31 Dec 2019)

2019-12-31 23:59:59
2019-12-31 23:59:59
Tuesday, December 31, 2019 23:59:59 (11:59:59 PM)

Wednesday, January 1, 2020 00:00:00 (12:00:00 AM)
Wednesday, January 1, 2020

2019-12-31 is earlier than 2020-01-01.
d is now 2020-01-01.

Chronological sort

The strptime function in line 20 of date2.py creates and returns an object of class datetime.datetime. (Unfortunately, there is no strptime function that creates and returns an object of class datetime.date.) Then the date function in line 21 creates and returns an object of class datetime.date that has the same year, month, and day as the datetime.datetime, but no hour, minute, or second.

date2.py

July 20, 1969
November 22, 1963
October 24, 1929
September 1, 1939
September 11, 2001

October 24, 1929
September 1, 1939
November 22, 1963
July 20, 1969
September 11, 2001

Things to try

  1. Remove the score function in lines 18–21 of date2.py and replace it with a lambda function in line 29.
    dates.sort(key = lambda item: datetime.datetime.strptime(item, "%B %d, %Y").date())
    

  2. Use a list comprehension to create a list of objects of class datetime.date, and then sort this new list without any key argument.
    listOfDateObjects = \
        [datetime.datetime.strptime(date, "%B %d, %Y").date() for date in dates]
    
    listOfDateObjects.sort()   #chronological order
    
    for date in listOfDateObjects:
        print(date)
    
    1929-10-24
    1939-09-01
    1963-11-22
    1969-07-20
    2001-09-11
    
  3. The following code outputs two different lines on macOS and Linux. Does it output two different lines on Microsoft Windows?
    import datetime
    
    d = datetime.date(2019, 12, 9)
    print(d.strftime("%A, %B %-d, %Y")) #on macOS: Monday, December 9, 2019
    print(d.strftime("%A, %B %d, %Y"))  #on macOS: Monday, December 09, 2019
    
    Monday, December 9, 2019
    Monday, December 09, 2019