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

Course Content

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

bookDatetime 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

Task

Create datetime object dt with date 28 June 2002 and time 14:58:41. Format this object to format "06/28/02 14:58".

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 5
toggle bottom row

bookDatetime 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

Task

Create datetime object dt with date 28 June 2002 and time 14:58:41. Format this object to format "06/28/02 14:58".

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 5
toggle bottom row

bookDatetime 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

Task

Create datetime object dt with date 28 June 2002 and time 14:58:41. Format this object to format "06/28/02 14:58".

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

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

Task

Create datetime object dt with date 28 June 2002 and time 14:58:41. Format this object to format "06/28/02 14:58".

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Section 2. Chapter 5
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
some-alt