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.
Soluzione
| 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 |
Grazie per i tuoi commenti!
single
Chieda ad AI
Chieda ad AI
Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione
Riassuma questo capitolo
Explain code
Explain why doesn't solve task
Awesome!
Completion rate improved to 3.23
Reading Date and Time from String
Scorri per mostrare il 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.
Soluzione
| 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 |
Grazie per i tuoi commenti!
single