2.1. Datetime Dates and Time

2.1.1. Creating date objects

Listing 2.37. Create date object with custom date
from datetime import date


d = date(1961, 4, 12)

d                   # datetime.date(1961, 4, 12)
d.year              # 1961
d.month             # 4
d.day               # 12
Listing 2.38. Create date object with current date
from datetime import date


today = date.today()

today               # datetime.date(2020, 1, 5)
today.year          # 2020
today.month         # 1
today.day           # 5

2.1.1.1. date methods

  • Python defines Monday as zero

  • ISO defines Monday as one

Listing 2.39. date object methods. Python defines Monday as zero. ISO defines Monday as one
from datetime import date


# Armstrong made a first step on the Moon on Monday
d = date(1969, 7, 21)

d                   # datetime.date(1969, 7, 21)
d.weekday()         # 0
d.isoweekday()      # 1
d.isoformat()       # '1969-07-21'

2.1.2. Creating time objects

Listing 2.40. Create time object with custom time
from datetime import time


t = time(2, 56, 15)
# datetime.time(2, 56, 15)

t.hour              # 2
t.minute            # 56
t.second            # 15
t.microsecond       # 0
Listing 2.41. Create time object representing midnight and noon
from datetime import time


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

time(12)            # datetime.time(12, 0)
time(12, 0)         # datetime.time(12, 0)
time(12, 0, 0)      # datetime.time(12, 0)

time(24, 0)         # ValueError: hour must be in 0..23

2.1.3. Creating datetime objects

Listing 2.42. Create datetime object
from datetime import datetime


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

dt                  # datetime.datetime(1969, 7, 21, 2, 56, 15)
dt.year             # 1969
dt.month            # 7
dt.day              # 21
dt.hour             # 2
dt.minute           # 56
dt.second           # 15
dt.microsecond      # 0
Listing 2.43. Create datetime with empty time (representing midnight)
from datetime import datetime


dt = datetime(1969, 7, 21)

df                  # datetime.datetime(1969, 7, 21, 0, 0, 0)
dt.year             # 1969
dt.month            # 7
dt.day              # 21
dt.hour             # 0
dt.minute           # 0
dt.second           # 0
dt.microsecond      # 0
Listing 2.44. Create datetime from date and time objects
from datetime import datetime, date, time


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

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

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

datetime.combine(d, t)
# datetime.datetime(1969, 7, 21, 2, 56, 15)

2.1.3.1. datetime methods

  • Python defines Monday as zero

  • ISO defines Monday as one

Listing 2.45. datetime methods
from datetime import datetime


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

dt                  # datetime.datetime(1969, 7, 21, 2, 56, 15)
dt.date()           # datetime.date(1969, 7, 21)
dt.time()           # datetime.time(2, 56, 15)
d.weekday()         # 0
d.isoweekday()      # 1
dt.isoformat()      # '1969-07-21T02:56:15'

2.1.3.2. Current datetime in local time

Listing 2.46. Current datetime in local timezone
from datetime import datetime


now = datetime.now()

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

2.1.4. Assignments

2.1.4.1. Datetime Create Custom

English
  1. Create date object with date of your birth

  2. Create time object with time of your birth

  3. Create datetime object with date and time of your birth

Polish
  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

2.1.4.2. Datetime Create Current

English
  1. Create date object with current date

  2. Create datetime object with current date and time

  3. Create time object with current time

  4. Date and time must be from system, not hardcoded in code

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

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

  3. Stwórz obiekt time z obecnym czasem

  4. Data i czas ma być pobierana z systemu, nie zapisana w kodzie