8.3. Datetime Create
datetime.datetime()- Date and timedatetime(year, month, day, hour, minute, second, microsecond)- create datetime
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:
>>> from datetime import datetime
>>>
>>> x = datetime(1969, 7, 21, 2, 56, 15)
>>> print(x)
1969-07-21 02:56:15
8.3.1. SetUp
>>> from datetime import datetime
8.3.2. 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
8.3.3. Split
datetime.date()- getdatefromdatetimedatetime.time()- gettimefromdatetime
>>> dt = datetime(1969, 7, 21, 2, 56, 15)
>>>
>>> dt.date()
datetime.date(1969, 7, 21)
>>>
>>> dt.time()
datetime.time(2, 56, 15)
8.3.4. Combine
datetime.combine(d, t)- createdatetimefromdateandtime
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.3.5. Empty Time
def datetime(year, month, day, hour=0, minute=0, second=0, microsecond=0)- create datetimeIf time part is empty, then it is midnight
>>> datetime(1969, 7, 21)
datetime.datetime(1969, 7, 21, 0, 0)
8.3.6. 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)
8.3.7. Use Case - 2
>>> 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)
8.3.8. Use Case - 3
>>> 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.3.9. Assignments
# %% About
# - Name: Datetime Datetime Create
# - Difficulty: easy
# - Lines: 1
# - Minutes: 1
# %% 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
# %% English
# 1. First human (Yuri Gagarin) flown to space on
# April 12th, 1961 at 6:07 a.m. from Bajkonur Cosmodrome in Kazakhstan
# 2. Define `result: datetime` to represent date and time of the launch
# 3. Use `datetime.datetime()`
# 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. Zdefiniuj `result: datetime` do reprezentacji daty i czasu startu
# 3. Użyj `datetime.datetime()`
# 4. Uruchom doctesty - wszystkie muszą się powieść
# %% Expected
# >>> result
# 1961-04-12 06:07:00
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'
>>> assert 'result' in globals(), \
'Variable `result` is not defined; assign result of your program to it.'
>>> assert result is not Ellipsis, \
'Variable `result` has an invalid value; assign result of your program to it.'
>>> assert type(result) is datetime, \
'Variable `result` has an invalid type; expected: `datetime`.'
>>> print(result)
1961-04-12 06:07:00
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`
# %% Imports
from datetime import datetime
# %% Types
result: datetime
# %% Data
# %% Result
result = ...
# %% About
# - Name: Datetime Datetime Attributes
# - Difficulty: easy
# - Lines: 7
# - Minutes: 3
# %% 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
# %% English
# 1. Use `DATA: datetime`
# 2. Define variables:
# - `result1: int` with year from `DATA`
# - `result2: int` with month from `DATA`
# - `result3: int` with day from `DATA`
# - `result4: int` with hour from `DATA`
# - `result5: int` with minute from `DATA`
# - `result6: int` with second from `DATA`
# - `result7: int` with microsecond from `DATA`
# 3. Run doctests - all must succeed
# %% Polish
# 1. Użyj `DATA: datetime`
# 2. Zdefiniuj zmienne:
# - `result1: int` z rokiem z `DATA`
# - `result2: int` z miesiącem z `DATA`
# - `result3: int` z dniem z `DATA`
# - `result4: int` z godziną z `DATA`
# - `result5: int` z minutą z `DATA`
# - `result6: int` z sekundą z `DATA`
# - `result7: int` z mikrosekundą z `DATA`
# 3. Uruchom doctesty - wszystkie muszą się powieść
# %% Expected
# >>> result1
# 1969
#
# >>> result2
# 7
#
# >>> result3
# 21
#
# >>> result4
# 2
#
# >>> result5
# 56
#
# >>> result6
# 15
#
# >>> result7
# 0
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'
>>> assert 'result1' in globals(), \
'Variable `result1` is not defined; assign result of your program to it.'
>>> assert result1 is not Ellipsis, \
'Variable `result1` has an invalid value; assign result of your program to it.'
>>> assert type(result1) is int, \
'Variable `result1` has an invalid type; expected: `int`.'
>>> result1
1969
>>> assert 'result2' in globals(), \
'Variable `result2` is not defined; assign result of your program to it.'
>>> assert result2 is not Ellipsis, \
'Variable `result2` has an invalid value; assign result of your program to it.'
>>> assert type(result2) is int, \
'Variable `result2` has an invalid type; expected: `int`.'
>>> result2
7
>>> assert 'result3' in globals(), \
'Variable `result3` is not defined; assign result of your program to it.'
>>> assert result3 is not Ellipsis, \
'Variable `result3` has an invalid value; assign result of your program to it.'
>>> assert type(result3) is int, \
'Variable `result3` has an invalid type; expected: `int`.'
>>> result3
21
>>> assert 'result4' in globals(), \
'Variable `result4` is not defined; assign result of your program to it.'
>>> assert result4 is not Ellipsis, \
'Variable `result4` has an invalid value; assign result of your program to it.'
>>> assert type(result4) is int, \
'Variable `result4` has an invalid type; expected: `int`.'
>>> result4
2
>>> assert 'result5' in globals(), \
'Variable `result5` is not defined; assign result of your program to it.'
>>> assert result5 is not Ellipsis, \
'Variable `result5` has an invalid value; assign result of your program to it.'
>>> assert type(result5) is int, \
'Variable `result5` has an invalid type; expected: `int`.'
>>> result5
56
>>> assert 'result6' in globals(), \
'Variable `result6` is not defined; assign result of your program to it.'
>>> assert result6 is not Ellipsis, \
'Variable `result6` has an invalid value; assign result of your program to it.'
>>> assert type(result6) is int, \
'Variable `result6` has an invalid type; expected: `int`.'
>>> result6
15
>>> assert 'result7' in globals(), \
'Variable `result7` is not defined; assign result of your program to it.'
>>> assert result7 is not Ellipsis, \
'Variable `result7` has an invalid value; assign result of your program to it.'
>>> assert type(result7) is int, \
'Variable `result7` has an invalid type; expected: `int`.'
>>> result7
0
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`
# %% Imports
from datetime import datetime
# %% Types
result1: int
result2: int
result3: int
result4: int
result5: int
result6: int
result7: int
# %% Data
DATA = datetime(1969, 7, 21, 2, 56, 15)
# %% Result
result1 = ...
result2 = ...
result3 = ...
result4 = ...
result5 = ...
result6 = ...
result7 = ...
# %% About
# - Name: Datetime Datetime Split
# - Difficulty: easy
# - Lines: 2
# - Minutes: 2
# %% 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
# %% English
# 1. First human (Yuri Gagarin) flown to space on
# April 12th, 1961 at 6:07 a.m. from Bajkonur Cosmodrome in Kazakhstan
# 2. Use `DATE: datetime`
# 3. Define variables:
# - `result1: date` to represent date of the launch
# - `result2: 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:
# - `result1: date` do reprezentacji datę startu
# - `result2: time` do reprezentacji czas startu
# 4. Uruchom doctesty - wszystkie muszą się powieść
# %% Expected
# >>> print(result1)
# 1961-04-12
#
# >>> print(result2)
# 06:07:00
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'
>>> assert 'result1' in globals(), \
'Variable `result1` is not defined; assign result of your program to it.'
>>> assert result1 is not Ellipsis, \
'Variable `result1` has an invalid value; assign result of your program to it.'
>>> assert type(result1) is date, \
'Variable `result1` has an invalid type; expected: `date`.'
>>> print(result1)
1961-04-12
>>> assert 'result2' in globals(), \
'Variable `result2` is not defined; assign result of your program to it.'
>>> assert result2 is not Ellipsis, \
'Variable `result2` has an invalid value; assign result of your program to it.'
>>> assert type(result2) is time, \
'Variable `result2` has an invalid type; expected: `time`.'
>>> print(result2)
06:07:00
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`
# %% Imports
from datetime import date, datetime, time
# %% Types
result1: date
result2: time
# %% Data
DATA = datetime(1961, 4, 12, 6, 7)
# %% Result
result1 = ...
result2 = ...
# %% About
# - Name: Datetime Datetime Combine
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2
# %% 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
# %% English
# 1. First human (Yuri Gagarin) flown to space on
# April 12th, 1961 at 6:07 a.m. from Bajkonur Cosmodrome in Kazakhstan
# 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ść
# %% Expected
# >>> print(result)
# 1961-04-12 06:07:00
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'
>>> assert 'result' in globals(), \
'Variable `result` is not defined; assign result of your program to it.'
>>> assert result is not Ellipsis, \
'Variable `result` has an invalid value; assign result of your program to it.'
>>> assert type(result) is datetime, \
'Variable `result` has an invalid type; expected: `datetime`.'
>>> print(result)
1961-04-12 06:07:00
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`
# %% Imports
from datetime import date, datetime, time
# %% Types
result: datetime
# %% Data
d = date(1961, 4, 12)
t = time(6, 7)
# %% Result
result = ...