Datetime Formats
Like we mentioned in the previous chapter since datetime object has also time added, there are additional format codes for datetime objects. The table with the most usable format codes is below.
| 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 |
For example, we can do the same things as in the previous chapter, but in a more representative format. That is, the May month with these codes will be represented as 05, which is better than just 5. Let's show how it works on some random date.
123456789# Load class from library from datetime import datetime # Create datetime object dt = datetime(2020, 11, 1, 11, 20, 25) # Format datetime object to some format dt_formatted = dt.strftime("%H:%M %d.%m.%Y") print(dt_formatted)
Swipe to start coding
Create datetime object dt with date 28 June 2002 and time 14:58:41. Format this object to format "06/28/02 14:58".
Oplossing
Bedankt voor je feedback!
single
Vraag AI
Vraag AI
Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.
Vat dit hoofdstuk samen
Explain code
Explain why doesn't solve task
Awesome!
Completion rate improved to 3.23
Datetime Formats
Veeg om het menu te tonen
Like we mentioned in the previous chapter since datetime object has also time added, there are additional format codes for datetime objects. The table with the most usable format codes is below.
| 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 |
For example, we can do the same things as in the previous chapter, but in a more representative format. That is, the May month with these codes will be represented as 05, which is better than just 5. Let's show how it works on some random date.
123456789# Load class from library from datetime import datetime # Create datetime object dt = datetime(2020, 11, 1, 11, 20, 25) # Format datetime object to some format dt_formatted = dt.strftime("%H:%M %d.%m.%Y") print(dt_formatted)
Swipe to start coding
Create datetime object dt with date 28 June 2002 and time 14:58:41. Format this object to format "06/28/02 14:58".
Oplossing
Bedankt voor je feedback!
single