Reading Date and Time from String
By this time we can convert dates and times into strings of desired formats. But there is one important moment - into strings. The functionality we learned before can not be applied to strings. So, there has to be a revert way.
Of course, it exists! Consider datetime object named test. We used test.strftime(pattern) to convert datetime object into some pattern string. To convert string s into datetime object you need to use datetime.strptime(s, pattern). This will convert the string into a default datetime object. The pattern within the function - is the pattern of string you want to convert into datetime object. So, there you need to detect all the necessary codes satisfying the string. For example, imagine we have the string "1 November, 2021". There we can see the day of the month, month name, and 4-digit year. Therefore the necessary codes are %d, %B, and %Y.
123456789# Load classes from library from datetime import datetime # String with date s = '1 November, 2021' # Parse string to datetime dt = datetime.strptime(s, "%d %B, %Y") print(f"String '{s}' converted to datetime: {dt}")
Swipe to start coding
You are given two strings d1 and d2 with dates and times. You need to convert them to datetime objects and save them in variables dt1 and dt2 respectively. Read the second string as May 23, 2019. Follow the table with the codes below, or take the hint to see the codes needed.
Solution
| Format code | Meaning | Example |
|---|---|---|
%d | Day of the month as a zero-padded decimal number | 01, 02, ..., 30, 31 |
%m | Month as a zero-padded decimal number | 01, 02, ..., 11, 12 |
%y | Year without century as a zero-padded decimal number | 00, 01, 02, ..., 98, 99 |
%Y | Year with century as a decimal number | 0001, 0002, 1999, 2000, 2001, ... |
%b | Month as localeβs abbreviated name | Jan, Feb, ..., Nov, Dec |
%B | Month as localeβs full name | January, February, ..., November, December |
%a | Weekday as localeβs abbreviated name | Sun, Mon, ..., Fri, Sat |
%A | Weekday as localeβs full name | Sunday, Monday, ..., Friday, Saturday |
%H | Hour (24-hour clock) as a zero-padded decimal number | 00, 01, ..., 23, 24 |
%M | Minute as a zero-padded decimal number | 00, 01, ..., 58, 59 |
%S | Second as a zero-padded decimal number | 00, 01, ..., 58, 59 |
%H | Hour (24-hour clock) as a zero-padded decimal number | 00, 01, ..., 22, 23 |
%I | Hour (12-hour clock) as a zero-padded decimal number | 01, 02, ..., 11, 12 |
%p | Localeβs equivalent of either AM or PM | AM, PM |
Thanks for your feedback!
single
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Summarize this chapter
Explain the code in file
Explain why file doesn't solve the task
Awesome!
Completion rate improved to 3.23
Reading Date and Time from String
Swipe to show menu
By this time we can convert dates and times into strings of desired formats. But there is one important moment - into strings. The functionality we learned before can not be applied to strings. So, there has to be a revert way.
Of course, it exists! Consider datetime object named test. We used test.strftime(pattern) to convert datetime object into some pattern string. To convert string s into datetime object you need to use datetime.strptime(s, pattern). This will convert the string into a default datetime object. The pattern within the function - is the pattern of string you want to convert into datetime object. So, there you need to detect all the necessary codes satisfying the string. For example, imagine we have the string "1 November, 2021". There we can see the day of the month, month name, and 4-digit year. Therefore the necessary codes are %d, %B, and %Y.
123456789# Load classes from library from datetime import datetime # String with date s = '1 November, 2021' # Parse string to datetime dt = datetime.strptime(s, "%d %B, %Y") print(f"String '{s}' converted to datetime: {dt}")
Swipe to start coding
You are given two strings d1 and d2 with dates and times. You need to convert them to datetime objects and save them in variables dt1 and dt2 respectively. Read the second string as May 23, 2019. Follow the table with the codes below, or take the hint to see the codes needed.
Solution
| Format code | Meaning | Example |
|---|---|---|
%d | Day of the month as a zero-padded decimal number | 01, 02, ..., 30, 31 |
%m | Month as a zero-padded decimal number | 01, 02, ..., 11, 12 |
%y | Year without century as a zero-padded decimal number | 00, 01, 02, ..., 98, 99 |
%Y | Year with century as a decimal number | 0001, 0002, 1999, 2000, 2001, ... |
%b | Month as localeβs abbreviated name | Jan, Feb, ..., Nov, Dec |
%B | Month as localeβs full name | January, February, ..., November, December |
%a | Weekday as localeβs abbreviated name | Sun, Mon, ..., Fri, Sat |
%A | Weekday as localeβs full name | Sunday, Monday, ..., Friday, Saturday |
%H | Hour (24-hour clock) as a zero-padded decimal number | 00, 01, ..., 23, 24 |
%M | Minute as a zero-padded decimal number | 00, 01, ..., 58, 59 |
%S | Second as a zero-padded decimal number | 00, 01, ..., 58, 59 |
%H | Hour (24-hour clock) as a zero-padded decimal number | 00, 01, ..., 22, 23 |
%I | Hour (12-hour clock) as a zero-padded decimal number | 01, 02, ..., 11, 12 |
%p | Localeβs equivalent of either AM or PM | AM, PM |
Thanks for your feedback!
single