8.1. Datetime Dates and Time

8.1.1. Creating date objects

Listing 285. 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 286. 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

8.1.1.1. date methods

  • Python defines Monday as zero

  • ISO defines Monday as one

Listing 287. 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'

8.1.2. Creating time objects

Listing 288. 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 289. 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

8.1.3. Creating datetime objects

Listing 290. 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 291. 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 292. 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)
Listing 293. Create datetime from date and time objects
from datetime import datetime, date, time


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

datetime(d.year, d.month, d.day, t.hour, t. minute, t.second)
# datetime.datetime(1969, 7, 21, 2, 56, 15)
Listing 294. Create datetime from date and time objects
from datetime import datetime, date, time


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

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

8.1.3.1. datetime methods

  • Python defines Monday as zero

  • ISO defines Monday as one

Listing 295. 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'

8.1.3.2. Current datetime in local time

Listing 296. 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

8.1.4. Assignments

8.1.4.1. Create date, time and datetime objects

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

8.1.4.2. Create current date and datetime objects

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