8.9. Datetime Parse — Python
Parsing - analyze (a sentence) into its parts and describe their syntactic roles.
8.9.1. Parsing dates
>>> from datetime import datetime
Datetime parsing from string:
>>> x = '1961-04-12 06:07' >>> >>> datetime.strptime(x, '%Y-%m-%d %H:%M') datetime.datetime(1961, 4, 12, 6, 7)
8.9.2. Leading Zero
Mind that while parsing dates without leading zero, you do not use %#H
or %-H as it was for formatting. One should simply use %H to capture
hour:
>>> x = '1961-04-12 6:07' >>> >>> datetime.strptime(x, '%Y-%m-%d %H:%M') datetime.datetime(1961, 4, 12, 6, 7)
8.9.3. String Fitting
If there are any other characters in the string, such as commas, brackets spaces, colons, dashes etc, they should be reflected in the format string.
>>> x = 'Apr 12th, 1961 6:07 am' >>> >>> datetime.strptime(x, '%b %dth, %Y %I:%M %p') datetime.datetime(1961, 4, 12, 6, 7)
>>> x = '12 April 1961 at 6:07 am' >>> >>> datetime.strptime(x, '%d %B %Y at %I:%M %p') datetime.datetime(1961, 4, 12, 6, 7)
Omitting any of those values will result with an error:
>>> x = '12 April 1961 at 6:07 am' >>> >>> datetime.strptime(x, '%d %B %Y %I:%M %p') Traceback (most recent call last): ValueError: time data '12 April 1961 at 6:07 am' does not match format '%d %B %Y %I:%M %p'
8.9.4. Time Zone
More information in Datetime Timezone
>>> x = '12 April 1961 6:07 UTC' >>> >>> datetime.strptime(x, '%d %B %Y %H:%M %Z') datetime.datetime(1961, 4, 12, 6, 7)
>>> x = '1961-04-12 6:07 local' >>> >>> datetime.strptime(x, '%Y-%m-%d %H:%M') Traceback (most recent call last): ValueError: unconverted data remains: local
>>> x = '1961-04-12 6:07 local' >>> >>> datetime.strptime(x, '%Y-%m-%d %H:%M %Z') Traceback (most recent call last): ValueError: time data '1961-04-12 6:07 local' does not match format '%Y-%m-%d %H:%M %Z'
>>> x = '1961-04-12 6:07 local' >>> >>> datetime.strptime(x, '%Y-%m-%d %H:%M local') datetime.datetime(1961, 4, 12, 6, 7)
8.9.5. Assignments
# %% About # - Name: Datetime Parse US # - Difficulty: easy # - Lines: 1 # - 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. Define `result: datetime` with parsed date `DATA` # 2. Run doctests - all must succeed # %% Polish # 1. Zdefiniuj `result: datetime` ze sparsowaną datą `DATA` # 2. Uruchom doctesty - wszystkie muszą się powieść # %% Expected # >>> result # datetime.datetime(1969, 7, 21, 0, 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 '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`.' >>> result datetime.datetime(1969, 7, 21, 0, 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 result: datetime # %% Data DATA = 'July 21, 1969' # %% Result result = ...
# %% About # - Name: Datetime Parse Ordinals # - Difficulty: easy # - Lines: 1 # - 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. Define `result: datetime` with parsed date `DATA` # 2. Run doctests - all must succeed # %% Polish # 1. Zdefiniuj `result: datetime` ze sparsowaną datą `DATA` # 2. Uruchom doctesty - wszystkie muszą się powieść # %% Expected # >>> result # datetime.datetime(1969, 7, 21, 0, 0) # %% Hints # - `%dst` # %% 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`.' >>> result datetime.datetime(1969, 7, 21, 0, 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 result: datetime # %% Data DATA = 'July 21st, 1969' # %% Result result = ...
# %% About # - Name: Datetime Parse List # - Difficulty: medium # - Lines: 8 # - 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. Define `result: list[datetime]` with parsed `DATA` dates # 2. Run doctests - all must succeed # %% Polish # 1. Zdefiniuj `result: list[datetime]` ze sparsowanymi datami `DATA` # 2. Uruchom doctesty - wszystkie muszą się powieść # %% Expected # >>> result # [datetime.datetime(1969, 7, 21, 0, 0), # datetime.datetime(1969, 7, 22, 0, 0), # datetime.datetime(1969, 7, 23, 0, 0)] # %% Hints # - `for ... in` # - `try ... except` # - `dt.strptime()` # - `list.append()` # %% Doctests """ >>> import sys; sys.tracebacklimit = 0 >>> assert sys.version_info >= (3, 9), \ 'Python has an is invalid version; expected: `3.9` or newer.' >>> from pprint import pprint >>> result = list(result) >>> 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 list, \ 'Variable `result` has an invalid type; expected: `list`.' >>> assert all(type(x) is datetime for x in result), \ 'Variable `result` has elements of an invalid type; all items should be: `datetime`.' >>> pprint(result, width=30) [datetime.datetime(1969, 7, 21, 0, 0), datetime.datetime(1969, 7, 22, 0, 0), datetime.datetime(1969, 7, 23, 0, 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 date, datetime # %% Types result: list[date] # %% Data DATA = [ 'July 21st, 1969', 'July 22nd, 1969', 'July 23rd, 1969', ] # %% Result result = ...