Overview:
-
The class method fromisoformat() creates a datetime object as per the string in ISO 8601 format.
-
With the ISO 8601 format several combinations of inputs are possible. Most frequently used ones are often the subset of the following full form: YYYY-MM-DDTHH:MM:YY±hh:mm. The trailing ±hh:mm specifies the UTC offset in hours and minutes.
-
There are also the less used ones like YYYY-WNN, Where W is the prefix and NN is the week number of the year.
-
When the day of a week is specified the format YYYY-WNN-D, W is the prefix and NN is the week number of the year and D is the day of the week.
Example 1:
# Example Python program that creates datetime from datetime import datetime # Create the date for Christmas day # Local time 3 PM on New year day # Specify 2 PM EST on New year day # Specify 11 AM UTC on New year day |
Output:
Christmas day: |
Example 2:
# Example Python program that creates a datetime from datetime import datetime # Create the first date of a given week # Create the nth day of a given week |
Output:
First day of the 25th week: 2025-06-16 00:00:00 Second day of the 25th week: 2025-06-17 00:00:00 |