11.1. Datetime Create

  • Date

  • Time

  • Datetime

11.1.1. Custom Date and Time

All date, time and datetime classes resides in datetime module in standard library. At first name may look weird to import class datetime from module with the same name, but this is pretty common Python practice. There is a lot of classes with the same name as the module.

First, lets import all the necessary classes:

>>> from datetime import date, time, datetime

Date objects represents a particular day. Example dates are Yuri Gagarin (first man to fly to space) launch date, or the first day when a person (Neil Armstrong) set foot on the Moon. In order to create a date object:

>>> x = date(1969, 7, 21)
>>> print(x)
1969-07-21

Time objects represents given time. For example our classes starts at 9:00 am. or Armstrong set foot on the Moon at 2:56:15 am.

>>> x = time(2, 56, 15)
>>> print(x)
02:56:15

The most important object is a datetime class. This represents a particular moment in time. A date and a time. For example, Armstrong set his foot on the Moon on: July 21st, 1969 at 2:56:15 am. Datetimes are typically associated with timezones (and the above event happen on that particular time of UTC timezone) but we will cover this topic later on:

>>> x = datetime(1969, 7, 21, 2, 56, 15)
>>> print(x)
1969-07-21 02:56:15

11.1.2. Date Attributes

Lets create a date object:

>>> from datetime import date
>>>
>>> x = date(1969, 7, 21)

You can access date attributes by . dot notation. Attributes stored in this class are: year, month, day:

>>> print(x.year)
1969
>>> print(x.month)
7
>>> print(x.day)
21

11.1.3. Time Attributes

Lets create a time object:

>>> from datetime import time
>>>
>>> t = time(2, 56, 15)

Time object, has different attributes than date object. This should be completely understandable since both objects represents different things, a date and time respectively.

With time object you can access hour, minute, second and microsecond attributes:

>>> t.hour
2
>>> t.minute
56
>>> t.second
15
>>> t.microsecond
0

11.1.4. Datetime Attributes

Lets create a time object:

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

Datetime object is the most versatile and the most commonly used. It combines all the attributes from both date and time objects. You can access all of those attributes using dot . notation. The attributes are: year, month, day, hour, minute, second, microsecond.

>>> dt.year
1969
>>>
>>> dt.month
7
>>>
>>> dt.day
21
>>> dt.hour
2
>>>
>>> dt.minute
56
>>>
>>> dt.second
15
>>>
>>> dt.microsecond
0

11.1.5. Current Date and Time

Create date object with current date:

>>> from datetime import date
>>>
>>>
>>> today = date.today()

Create datetime object with current date and time:

>>> from datetime import datetime
>>>
>>>
>>> now = datetime.now()

There is no time.now()

11.1.6. Combine

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)

11.1.7. Split

>>> from datetime import datetime
>>>
>>>
>>> dt = datetime(1969, 7, 21, 2, 56, 15)
>>>
>>> dt.date()
datetime.date(1969, 7, 21)
>>>
>>> dt.time()
datetime.time(2, 56, 15)

11.1.8. Empty Time

>>> 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)
Traceback (most recent call last):
ValueError: hour must be in 0..23
>>> from datetime import datetime
>>>
>>>
>>> datetime(1969, 7, 21)
datetime.datetime(1969, 7, 21, 0, 0)

11.1.9. Use Case - 0x01

>>> 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)

11.1.10. Assignments

Code 11.22. Solution
"""
* Assignment: Datetime Create Custom
* Complexity: easy
* Lines of code: 3 lines
* Time: 2 min

English:
    1. First human (Yuri Gagarin) flown to space on
       April 12th, 1961 at 6:07 a.m. from Bajkonur Cosmodrome in Kazahstan
    2. Define `d: date` to represent date of the launch
    3. Define `t: time` to represent time of the launch
    4. Define `dt: datetime` to represent time of the launch
    5. Do not use: `datetime.combine()`, `datetime.date()`, `datetime.time()`
    6. Run doctests - all must succeed

Polish:
    1. Pierwszy człowiek (Juri Gagarin) poleciał w kosmos
       12 kwietnia 1961 roku o 6:07 rano z kosmodromu Bajkonur w Kazachstanie
    2. Zdefiniuj `d: date` do reprezentacji datę startu
    3. Zdefiniuj `t: time` do reprezentacji czas startu
    4. Zdefiniuj `dt: datetime` do reprezentacji czas startu
    5. Nie używaj: `datetime.combine()`, `datetime.date()`, `datetime.time()`
    6. Uruchom doctesty - wszystkie muszą się powieść

Tests:
    >>> import sys; sys.tracebacklimit = 0

    >>> assert dt is not Ellipsis, \
    'Assign result to variable: `dt`'
    >>> assert d is not Ellipsis, \
    'Assign result to variable: `d`'
    >>> assert t is not Ellipsis, \
    'Assign result to variable: `t`'
    >>> assert type(dt) is datetime, \
    'Variable `dt` has invalid type, must be a datetime'
    >>> assert type(d) is date, \
    'Variable `d` has invalid type, must be a date'
    >>> assert type(t) is time, \
    'Variable `t` has invalid type, must be a time'

    >>> print(d)
    1961-04-12

    >>> print(t)
    06:07:00

    >>> print(dt)
    1961-04-12 06:07:00
"""

from datetime import date, datetime, time


# representing April 12th, 1961 6:07 a.m.
# type: datetime
dt = ...

# representing April 12th, 1961 6:07 a.m.
# type: date
d = ...

# representing April 12th, 1961 6:07 a.m.
# type: time
t = ...

Code 11.23. Solution
"""
* Assignment: Datetime Create Combine
* Complexity: easy
* Lines of code: 1 lines
* Time: 2 min

English:
    1. First human (Yuri Gagarin) flown to space on
       April 12th, 1961 at 6:07 a.m. from Bajkonur Cosmodrome in Kazahstan
    2. Combine date and time into `datetime` object representing
       exact moment of the launch
    3. Use `datetime.combine()`
    4. Run doctests - all must succeed

Polish:
    1. Pierwszy człowiek (Juri Gagarin) poleciał w kosmos
       12 kwietnia 1961 roku o 6:07 rano z kosmodromu Bajkonur w Kazachstanie
    2. Połącz datę i czas w obiekt `datetime` reprezentujący
       dokładny moment startu
    3. Użyj `datetime.combine()`
    4. Uruchom doctesty - wszystkie muszą się powieść

Tests:
    >>> import sys; sys.tracebacklimit = 0

    >>> assert result is not Ellipsis, \
    'Assign result to variable: `result`'
    >>> assert type(result) is datetime, \
    'Variable `result` has invalid type, must be a datetime'

    >>> print(result)
    1961-04-12 06:07:00
"""

from datetime import date, datetime, time

d = date(1961, 4, 12)
t = time(6, 7)

# combine d and t to represent April 12th, 1961 6:07 a.m.
# type: datetime
result = ...

Code 11.24. Solution
"""
* Assignment: Datetime Create Current
* Complexity: easy
* Lines of code: 3 lines
* Time: 2 min

English:
    1. Create `datetime` object with current date and time
    2. Create `date` object with current date
    3. Create `time` object with current time
    4. Date and time must be from system, not hardcoded in code
    5. Run doctests - all must succeed

Polish:
    1. Stwórz obiekt `datetime` z obecną datą i czasem
    2. Stwórz obiekt `date` z obecną datą
    3. Stwórz obiekt `time` z obecnym czasem
    4. Data i czas ma być pobierana z systemu, nie zapisana w kodzie
    5. Uruchom doctesty - wszystkie muszą się powieść

Tests:
    >>> import sys; sys.tracebacklimit = 0

    >>> assert d is not Ellipsis, \
    'Assign result to variable: `d`'
    >>> assert t is not Ellipsis, \
    'Assign result to variable: `t`'
    >>> assert dt is not Ellipsis, \
    'Assign result to variable: `dt`'
    >>> assert type(dt) is datetime, \
    'Variable `dt` has invalid type, must be a datetime'
    >>> assert type(d) is date, \
    'Variable `dt` has invalid type, must be a date'
    >>> assert type(t) is time, \
    'Variable `t` has invalid type, must be a time'
    >>> assert dt != datetime(1, 1, 1)
    >>> assert d != date(1, 1, 1)
    >>> assert t != time()
"""

from datetime import date, datetime, time


# representing current moment
# type: datetime
dt = ...

# representing current moment
# type: date
d = ...

# representing current moment
# type: time
t = ...