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

Conteúdo do Curso

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

Tarefa

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 desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 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

Tarefa

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 desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 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

Tarefa

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 desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu 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

Tarefa

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 desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Seção 2. Capítulo 5
Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
some-alt