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

Kursinnehåll

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
Uppgift

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 desktopByt till skrivbordet för praktisk övningFortsätt där du är med ett av alternativen nedan
Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 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
Uppgift

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 desktopByt till skrivbordet för praktisk övningFortsätt där du är med ett av alternativen nedan
Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 5
Switch to desktopByt till skrivbordet för praktisk övningFortsätt där du är med ett av alternativen nedan
Vi beklagar att något gick fel. Vad hände?
some-alt