8.1. Datetime Date

  • datetime.date() - Date

  • date(year, month, day) - create date

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:

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

8.1.1. SetUp

>>> from datetime import date

8.1.2. Date Attributes

  • date.year - year

  • date.month - month

  • date.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

8.1.3. Assignments

# %% About
# - Name: Datetime Date 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: date` to represent date of the launch
# 3. Use `datetime.date()`
# 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: date` do reprezentacji daty startu
# 3. Use `datetime.date()`
# 4. Uruchom doctesty - wszystkie muszą się powieść

# %% Expected
# >>> result
# 1961-04-12

# %% 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 date, \
'Variable `result` has an invalid type; expected: `date`.'

>>> print(result)
1961-04-12
"""

# %% 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

# %% Types
result: date

# %% Data

# %% Result
result = ...

# %% About
# - Name: Datetime Date Attributes
# - Difficulty: easy
# - Lines: 3
# - 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. Use `DATA: date`
# 2. Define variables:
#    - `result1: int` with year from `DATA`
#    - `result2: int` with month from `DATA`
#    - `result3: int` with day from `DATA`
# 3. Run doctests - all must succeed

# %% Polish
# 1. Użyj `DATA: date`
# 2. Zdefiniuj zmienne:
#    - `result1: int` z rokiem z `DATA`
#    - `result2: int` z miesiącem z `DATA`
#    - `result3: int` z dniem z `DATA`
# 3. Uruchom doctesty - wszystkie muszą się powieść

# %% Expected
# >>> result1
# 1969
#
# >>> result2
# 7
#
# >>> result3
# 21

# %% 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
"""

# %% 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

# %% Types
result1: int
result2: int
result3: int

# %% Data
DATA = date(1969, 7, 21)

# %% Result
result1 = ...
result2 = ...
result3 = ...