1. Dates and Time

1.1. Creating date objects

1.1.1. Create date

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

Listing 148. Current date
from datetime import date


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

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

1.1.3. date methods

Listing 149. date methods
from datetime import date


d = date(1969, 7, 21)

d.weekday()         # 0  # in US week starts with Sunday
d.isoweekday()      # 1
d.isoformat()       # '1969-07-21'

1.2. Creating time objects

1.2.1. Create time

Listing 150. Create 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

1.2.2. Noon and midnight

Listing 151. Create empty time - midnight
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)

1.3. Creating datetime objects

1.3.1. Create datetime

Listing 152. Create datetime
from datetime import datetime


dt = datetime(1969, 7, 21, 2, 56, 15)
# 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

1.3.2. Create datetime with empty time (midnight)

Listing 153. Create datetime with empty time
from datetime import datetime


dt = datetime(1969, 7, 21)
# 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

1.3.3. Create datetime from date and time objects

Listing 154. Create datetime from date and time objects
from datetime import datetime, date, time


d = date(1969, 7, 21)
t = time(2, 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, 2, 56, 15)
Listing 155. Create datetime from date and time objects
from datetime import datetime, date, time


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

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

1.3.4. datetime methods

Listing 156. datetime methods
from datetime import datetime


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

dt.date()        # datetime.date(1969, 7, 21)
dt.time()        # datetime.time(2, 56, 15)

dt.weekday()     # 0  # in US week starts with Sunday
dt.isoweekday()  # 1
dt.isoformat()   # '1969-07-21T02:56:15'

1.3.5. Current datetime in local time

Listing 157. 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. Assignments

1.4.1. Create current date and datetime objects

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

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

  3. Jak stworzyć obiekt time obecnym czasem?

1.4.2. Create date, time and datetime objects

  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