8.4. Datetime Current

  • datetime.date() - Date

  • datetime.time() - Time

  • datetime.datetime() - Date and time

8.4.1. SetUp

>>> from datetime import date, time, datetime

8.4.2. Current Date and Time

  • datetime.now() - current date and time

Create datetime object with current date and time:

>>> x = datetime.now()
>>>
>>> print(x)
2000-01-02 02:03:04.000005

8.4.3. Current Date

  • date.today() - current date

Create date object with current date:

>>> x = date.today()
>>>
>>> print(x)
2000-01-02

Or:

>>> x = datetime.now()
>>>
>>> x.date()
2000-01-02

8.4.4. Current Time

  • There is no time.now()

There is no time.now()

>>> x = datetime.now()
>>>
>>> x.time()
02:03:04.000005

8.4.5. Assignments

# %% About
# - Name: Datetime Create Current
# - 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. Define `result1: datetime` with current date and time
# 2. Define `result2: date` with current date
# 3. Define `result3: time` with current time
# 4. Date and time must be from system, not hardcoded in code
# 5. Run doctests - all must succeed

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

# %% Expected
# >>> type(result1)
# <class 'datetime.datetime'>
#
# >>> type(result2)
# <class 'datetime.date'>
#
# >>> type(result3)
# <class 'datetime.time'>

# %% 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 '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 '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(result1) is datetime, \
'Variable `result1` has an invalid type; expected: `datetime`.'

>>> assert type(result2) is date, \
'Variable `result2` has an invalid type; expected: `date`.'

>>> assert type(result3) is time, \
'Variable `result3` has an invalid type; expected: `time`.'

>>> assert result1 != datetime(1, 1, 1)
>>> assert result2 != date(1, 1, 1)
>>> assert result3 != time()

>>> type(result1)
<class 'datetime.datetime'>

>>> type(result2)
<class 'datetime.date'>

>>> type(result3)
<class 'datetime.time'>
"""

# %% 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: datetime
result2: date
result3: time

# %% Data

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