1. Dates and Time

1.1. Creating date objects

1.1.1. Create date

Listing 153. date object and its properties
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 154. date object with 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 155. date object 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 156. time object and its properties
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 157. time object representing 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 158. 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 159. 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 160. 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 161. 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)
Listing 162. 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.combine(d, t)
# datetime.datetime(1969, 7, 21, 2, 56, 15)

1.3.4. datetime methods

Listing 163. 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 164. 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 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

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