Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Datetime Formats | Working with Times
Dealing with Dates and Times in Python
course content

Kursusindhold

Dealing with Dates and Times in Python

Dealing with Dates and Times in Python

1. Working with Dates
2. Working with Times
3. Timezones and Daylight Savings Time (DST)
4. Working with Dates and Times in pandas

book
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 codeMeaningExample
%dDay of the month as a zero-padded decimal number01, 02, ..., 30, 31
%mMonth as a zero-padded decimal number01, 02, ..., 11, 12
%yYear without century as a zero-padded decimal number00, 01, 02, ..., 98, 99
%YYear with century as a decimal number0001, 0002, 1999, 2000, 2001, ...
%bMonth as locale’s abbreviated nameJan, Feb, ..., Nov, Dec
%BMonth as locale’s full nameJanuary, February, ..., November, December
%aWeekday as locale’s abbreviated nameSun, Mon, ..., Fri, Sat
%AWeekday as locale’s full nameSunday, Monday, ..., Friday, Saturday
%HHour (24-hour clock) as a zero-padded decimal number00, 01, ..., 23, 24
%MMinute as a zero-padded decimal number00, 01, ..., 58, 59
%SSecond as a zero-padded decimal number00, 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)
copy
Opgave

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".

Løsning

Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 5
toggle bottom row

book
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 codeMeaningExample
%dDay of the month as a zero-padded decimal number01, 02, ..., 30, 31
%mMonth as a zero-padded decimal number01, 02, ..., 11, 12
%yYear without century as a zero-padded decimal number00, 01, 02, ..., 98, 99
%YYear with century as a decimal number0001, 0002, 1999, 2000, 2001, ...
%bMonth as locale’s abbreviated nameJan, Feb, ..., Nov, Dec
%BMonth as locale’s full nameJanuary, February, ..., November, December
%aWeekday as locale’s abbreviated nameSun, Mon, ..., Fri, Sat
%AWeekday as locale’s full nameSunday, Monday, ..., Friday, Saturday
%HHour (24-hour clock) as a zero-padded decimal number00, 01, ..., 23, 24
%MMinute as a zero-padded decimal number00, 01, ..., 58, 59
%SSecond as a zero-padded decimal number00, 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)
copy
Opgave

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".

Løsning

Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 5
Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Vi beklager, at noget gik galt. Hvad skete der?
some-alt