1. Dates and Time

1.1. Creating date objects

1.1.1. Create date

Code Listing 1.6. Create date
from datetime import date


d = date(1961, 4, 12)
# datetime.date(1961, 4, 12)

d.year    # 1961
d.month   # 4
d.day     # 12

1.1.2. Current date

Code Listing 1.7. Current date
from datetime import date


today = date.today()
# datetime.date(2019, 1, 5)

today.year    # 2019
today.month   # 1
today.day     # 5

1.2. Creating time objects

1.2.1. Create time

Code Listing 1.8. Create time
from datetime import time


t = time(12, 33, 44)
# datetime.time(12, 33, 44)

t.hour            # 12
t.minute          # 33
t.second          # 44
t.microsecond     # 0

1.2.2. Create empty time (midnight)

Code Listing 1.9. Create empty time - midnight
from datetime import time


midnight = time()
# datetime.time(0, 0)

1.3. Creating datetime objects

1.3.1. Create datetime

Code Listing 1.10. Create datetime
from datetime import datetime


dt = datetime(1969, 7, 21, 14, 56, 15)

dt.year          # 1969
dt.month         # 7
dt.day           # 21
dt.hour          # 14
dt.minute        # 56
dt.second        # 15
dt.microsecond   # 0

1.3.2. Create datetime with empty time (midnight)

Code Listing 1.11. Create datetime with empty time
from datetime import datetime


dt = datetime(1969, 7, 21)

dt.year          # 1969
dt.month         # 7
dt.day           # 21
dt.hour          # 0
dt.minute        # 0
dt.second        # 0
dt.microsecond   # 0

1.3.3. Create datetime from date and time objects

Code Listing 1.12. Create datetime from date and time objects
from datetime import datetime, date, time


d = date(1969, 7, 21)
t = time(14, 56, 15)

dt = datetime(
    year=d.year,
    month=d.month,
    day=d.day,
    hour=t.hour,
    minute=t. minute,
    second=t.second)
# datetime.datetime(1969, 7, 21, 14, 56, 15)


dt = datetime(d.year, d.month, d.day, t.hour, t. minute, t.second)
# datetime.datetime(1969, 7, 21, 14, 56, 15)

1.3.4. Current datetime in local time

Code Listing 1.13. Current datetime in local timezone
from datetime import datetime


now = datetime.now()
# datetime.datetime(2019, 1, 5, 20, 15, 0, 547414)

now.year          # 2019
now.month         # 1
now.day           # 5
now.hour          # 20
now.minute        # 15
now.second        # 0
now.microsecond   # 547414

1.4. datetime methods

Code Listing 1.14. datetime methods
from datetime import datetime


dt = datetime(1969, 7, 21, 14, 56, 15)

dt.date()        # datetime.date(1969, 7, 21)
dt.time()        # datetime.time(14, 56, 15)
dt.weekday()     # 0  # in US week starts with Sunday

1.5. Assignments

1.5.1. Create date, time and datetime objects

  • Filename: datetime_create_custom.py

  • Lines of code to write: 3 lines

  • Estimated time of completion: 5 min

  1. Stwórz obiekt date z datą Twojego urodzenia

  2. Stwórz obiekt time z czasem Twojego urodzenia

  3. Stwórz obiekt datetime z datą i czasem Twojego urodzenia

1.5.2. Create current date and datetime objects

  • Filename: datetime_create_current.py

  • Lines of code to write: 3 lines

  • Estimated time of completion: 5 min

  1. Stwórz obiekt date z obecną datą

  2. Stwórz obiekt datetime z datą i czasem

  3. Jak stworzyć obiekt time obecnym czasem?