7.1. Datetime Create
datetime.date()
- Datedatetime.time()
- Timedatetime.datetime()
- Date and time
7.1.1. SetUp
>>> from datetime import date, time, datetime
7.1.2. Custom Date and Time
date(year, month, day)
- create datetime(hour, minute, second, microsecond)
- create timedatetime(year, month, day, hour, minute, second, microsecond)
- create datetime
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.
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
7.1.3. Date Attributes
date.year
- yeardate.month
- monthdate.day
- day
Lets create a date object:
>>> 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
7.1.4. Time Attributes
time.hour
- hourtime.minute
- minutetime.second
- secondtime.microsecond
- microsecond
Lets create a time object:
>>> 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
7.1.5. Datetime Attributes
datetime.year
- yeardatetime.month
- monthdatetime.day
- daydatetime.hour
- hourdatetime.minute
- minutedatetime.second
- seconddatetime.microsecond
- microsecond
Lets create a time object:
>>> 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
7.1.6. Current Date and Time
datetime.now()
- current date and timedate.today()
- current dateThere is no
time.now()
Create date
object with current date:
>>> today = date.today()
Create datetime
object with current date and time:
>>> now = datetime.now()
There is no time.now()
7.1.7. Combine
datetime.combine(d, t)
- createdatetime
fromdate
andtime
Create datetime
from date
and time
objects:
>>> d = date(1969, 7, 21)
>>> t = time(2, 56, 15)
>>>
>>> datetime.combine(d, t)
datetime.datetime(1969, 7, 21, 2, 56, 15)
7.1.8. Split
datetime.date()
- getdate
fromdatetime
datetime.time()
- gettime
fromdatetime
>>> dt = datetime(1969, 7, 21, 2, 56, 15)
>>>
>>> dt.date()
datetime.date(1969, 7, 21)
>>>
>>> dt.time()
datetime.time(2, 56, 15)
7.1.9. Empty Time
def time(hour=0, minute=0, second=0, microsecond=0)
- create timedef datetime(year, month, day, hour=0, minute=0, second=0, microsecond=0)
- create datetimetime()
- midnight
>>> 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
>>> datetime(1969, 7, 21)
datetime.datetime(1969, 7, 21, 0, 0)
7.1.10. Use Case - 1
>>> 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)
7.1.11. Assignments
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: Datetime Create Custom
# - Difficulty: easy
# - Lines: 3
# - Minutes: 2
# %% 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 sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> 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
# Represent date and time of the Gagarin's launch: April 12th, 1961 6:07 a.m.
# type: datetime
dt = ...
# Represent date of the Gagarin's launch: April 12th, 1961 6:07 a.m.
# type: date
d = ...
# Represent time of the Gagarin's launch: April 12th, 1961 6:07 a.m.
# type: time
t = ...
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: Datetime Create Attributes
# - Difficulty: easy
# - Lines: 7
# - Minutes: 3
# %% English
# 1. Use `DATA: datetime`
# 2. Define variables:
# - `result_a: int` with year from `DATA`
# - `result_b: int` with month from `DATA`
# - `result_c: int` with day from `DATA`
# - `result_d: int` with hour from `DATA`
# - `result_e: int` with minute from `DATA`
# - `result_f: int` with second from `DATA`
# - `result_g: int` with microsecond from `DATA`
# 3. Run doctests - all must succeed
# %% Polish
# 1. Użyj `DATA: datetime`
# 2. Zdefiniuj zmienne:
# - `result_a: int` z rokiem z `DATA`
# - `result_b: int` z miesiącem z `DATA`
# - `result_c: int` z dniem z `DATA`
# - `result_d: int` z godziną z `DATA`
# - `result_e: int` z minutą z `DATA`
# - `result_f: int` z sekundą z `DATA`
# - `result_g: int` z mikrosekundą z `DATA`
# 3. Uruchom doctesty - wszystkie muszą się powieść
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> assert result_a is not Ellipsis, \
'Assign result to variable: `result_a`'
>>> assert type(result_a) is int, \
'Variable `result_a` has invalid type, must be an int'
>>> result_a
1969
>>> assert result_b is not Ellipsis, \
'Assign result to variable: `result_b`'
>>> assert type(result_b) is int, \
'Variable `result_b` has invalid type, must be an int'
>>> result_b
7
>>> assert result_c is not Ellipsis, \
'Assign result to variable: `result_c`'
>>> assert type(result_c) is int, \
'Variable `result_c` has invalid type, must be an int'
>>> result_c
21
>>> assert result_d is not Ellipsis, \
'Assign result to variable: `result_d`'
>>> assert type(result_d) is int, \
'Variable `result_d` has invalid type, must be an int'
>>> result_d
2
>>> assert result_e is not Ellipsis, \
'Assign result to variable: `result_e`'
>>> assert type(result_e) is int, \
'Variable `result_e` has invalid type, must be an int'
>>> result_e
56
>>> assert result_f is not Ellipsis, \
'Assign result to variable: `result_f`'
>>> assert type(result_f) is int, \
'Variable `result_f` has invalid type, must be an int'
>>> result_f
15
>>> assert result_g is not Ellipsis, \
'Assign result to variable: `result_g`'
>>> assert type(result_g) is int, \
'Variable `result_g` has invalid type, must be an int'
>>> result_g
0
"""
from datetime import datetime
DATA = datetime(1969, 7, 21, 2, 56, 15)
# year from `DATA`
# type: int
result_a = ...
# month from `DATA`
# type: int
result_b = ...
# day from `DATA`
# type: int
result_c = ...
# hour from `DATA`
# type: int
result_d = ...
# minute from `DATA`
# type: int
result_e = ...
# second from `DATA`
# type: int
result_f = ...
# microsecond from `DATA`
# type: int
result_g = ...
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: Datetime Create Split
# - Difficulty: easy
# - Lines: 2
# - Minutes: 2
# %% English
# 1. First human (Yuri Gagarin) flown to space on
# April 12th, 1961 at 6:07 a.m. from Bajkonur Cosmodrome in Kazahstan
# 2. Use `DATE: datetime`
# 3. Define variables:
# - `result_a: date` to represent date of the launch
# - `result_b: time` to represent time of the launch
# 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. Użyj `DATE: datetime`
# 3. Zdefiniuj zmienne:
# - `result_a: date` do reprezentacji datę startu
# - `result_b: time` do reprezentacji czas startu
# 4. Uruchom doctesty - wszystkie muszą się powieść
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> assert result_a is not Ellipsis, \
'Assign result to variable: `result_a`'
>>> assert type(result_a) is date, \
'Variable `result_a` has invalid type, must be a date'
>>> print(result_a)
1961-04-12
>>> assert result_b is not Ellipsis, \
'Assign result to variable: `result_b`'
>>> assert type(result_b) is time, \
'Variable `result_b` has invalid type, must be a time'
>>> print(result_b)
06:07:00
"""
from datetime import date, datetime, time
DATA = datetime(1961, 4, 12, 6, 7)
# date of the launch
# type: date
result_a = ...
# time of the launch
# type: time
result_b = ...
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: Datetime Create Combine
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2
# %% 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 sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> 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 = ...
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: Datetime Create Current
# - Difficulty: easy
# - Lines: 3
# - Minutes: 2
# %% 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 sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> 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
# variable representing current date and time
# type: datetime
dt = ...
# variable representing current date
# type: date
d = ...
# variable representing current time
# type: time
t = ...