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 Datetime

  • datetime.now() - current date and time

Create datetime object with current date and time:

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

8.4.3. Current Date

  • date.today() - current date

Create date object with current date:

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

8.4.4. Current Time

  • There is no time.now()

There is no time.now(), but you can get current time from datetime.now():

>>> x = datetime.now().time()
>>>
>>> print(x)
03:04:05.000006

8.4.5. Assignments

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

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

# %% Expected
# >>> type(result)
# <class 'datetime.datetime'>

# %% 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`.'

>>> assert result != datetime(1, 1, 1)

>>> type(result)
<class 'datetime.datetime'>
"""

# %% 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 Current Date
# - 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. Define `result: date` with current date
# 2. Date must be from system, not hardcoded in code
# 3. Run doctests - all must succeed

# %% Polish
# 1. Zdefiniuj `result: date` z obecną datą
# 2. Data ma być pobierana z systemu, nie zapisana w kodzie
# 3. Uruchom doctesty - wszystkie muszą się powieść

# %% Expected
# >>> type(result)
# <class 'datetime.date'>

# %% 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`.'

>>> assert result != date(1, 1, 1)

>>> type(result)
<class 'datetime.date'>
"""

# %% 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 Current Time
# - 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. Define `result: time` with current time
# 2. Date and time must be from system, not hardcoded in code
# 3. Run doctests - all must succeed

# %% Polish
# 1. Zdefiniuj `result: time` z obecnym czasem
# 2. Data i czas ma być pobierana z systemu, nie zapisana w kodzie
# 3. Uruchom doctesty - wszystkie muszą się powieść

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

>>> assert result != time()

>>> type(result)
<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 datetime, time

# %% Types
result3: time

# %% Data

# %% Result
result = ...