Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Date Formats | Working with Dates
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

bookDate Formats

There are several date formats applied across the world. Also sometimes we want to convert a date into some readable or presentable format. For example, we can rewrite the same date in multiple ways: "01.11.2021", "11/01/2021", "11 Nov, 2021", "2021-11-01", and so on... How can we transform dates so they still will be interpreted by Python?

So, how can we transform the date object into another format? At first, we need to load datetime class from the datetime library within a new line (yes, sounds tautological). Then, you need to apply .strftime() method of datetime class (datetime.strftime()) with a date as the first argument, and pattern as the second. There where the complexity starts. A pattern - is a string, that can be built using the next format codes (the list is not full):

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

Let's build some examples. For example, we can create a date object representing the 28th of June, 2012. And let's transform it into two forms: "June 28, 2012" and "2012/06/28". Consider the first format: there we need full month name (B), day of the month (%d) and 4-digit year (%Y). So the pattern will look like "%B %d, %Y". In a similar way the second pattern can be built: 4-digit year (%Y), month as a zero-padded decimal number (%m), and day of the month (%d). In this case, the pattern will look like "%Y/%m/%d".

1234567891011
# Load classes from library from datetime import date from datetime import datetime # Create date object some_date = date(2012, 6, 28) # Format date into the first format print("Transformed into the first format date:", datetime.strftime(some_date, "%B %d, %Y")) # Format date into the first format print("Transformed into the second format date:", datetime.strftime(some_date, "%Y/%m/%d"))
copy
Tarefa
test

Swipe to show code editor

Given date object (1998-03-11) assigned to variable day. Using .strftime() method format day to format: "11.03.98" and save the result in day_formatted variable.

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 1. Capítulo 5
toggle bottom row

bookDate Formats

There are several date formats applied across the world. Also sometimes we want to convert a date into some readable or presentable format. For example, we can rewrite the same date in multiple ways: "01.11.2021", "11/01/2021", "11 Nov, 2021", "2021-11-01", and so on... How can we transform dates so they still will be interpreted by Python?

So, how can we transform the date object into another format? At first, we need to load datetime class from the datetime library within a new line (yes, sounds tautological). Then, you need to apply .strftime() method of datetime class (datetime.strftime()) with a date as the first argument, and pattern as the second. There where the complexity starts. A pattern - is a string, that can be built using the next format codes (the list is not full):

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

Let's build some examples. For example, we can create a date object representing the 28th of June, 2012. And let's transform it into two forms: "June 28, 2012" and "2012/06/28". Consider the first format: there we need full month name (B), day of the month (%d) and 4-digit year (%Y). So the pattern will look like "%B %d, %Y". In a similar way the second pattern can be built: 4-digit year (%Y), month as a zero-padded decimal number (%m), and day of the month (%d). In this case, the pattern will look like "%Y/%m/%d".

1234567891011
# Load classes from library from datetime import date from datetime import datetime # Create date object some_date = date(2012, 6, 28) # Format date into the first format print("Transformed into the first format date:", datetime.strftime(some_date, "%B %d, %Y")) # Format date into the first format print("Transformed into the second format date:", datetime.strftime(some_date, "%Y/%m/%d"))
copy
Tarefa
test

Swipe to show code editor

Given date object (1998-03-11) assigned to variable day. Using .strftime() method format day to format: "11.03.98" and save the result in day_formatted variable.

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 1. Capítulo 5
toggle bottom row

bookDate Formats

There are several date formats applied across the world. Also sometimes we want to convert a date into some readable or presentable format. For example, we can rewrite the same date in multiple ways: "01.11.2021", "11/01/2021", "11 Nov, 2021", "2021-11-01", and so on... How can we transform dates so they still will be interpreted by Python?

So, how can we transform the date object into another format? At first, we need to load datetime class from the datetime library within a new line (yes, sounds tautological). Then, you need to apply .strftime() method of datetime class (datetime.strftime()) with a date as the first argument, and pattern as the second. There where the complexity starts. A pattern - is a string, that can be built using the next format codes (the list is not full):

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

Let's build some examples. For example, we can create a date object representing the 28th of June, 2012. And let's transform it into two forms: "June 28, 2012" and "2012/06/28". Consider the first format: there we need full month name (B), day of the month (%d) and 4-digit year (%Y). So the pattern will look like "%B %d, %Y". In a similar way the second pattern can be built: 4-digit year (%Y), month as a zero-padded decimal number (%m), and day of the month (%d). In this case, the pattern will look like "%Y/%m/%d".

1234567891011
# Load classes from library from datetime import date from datetime import datetime # Create date object some_date = date(2012, 6, 28) # Format date into the first format print("Transformed into the first format date:", datetime.strftime(some_date, "%B %d, %Y")) # Format date into the first format print("Transformed into the second format date:", datetime.strftime(some_date, "%Y/%m/%d"))
copy
Tarefa
test

Swipe to show code editor

Given date object (1998-03-11) assigned to variable day. Using .strftime() method format day to format: "11.03.98" and save the result in day_formatted variable.

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!

There are several date formats applied across the world. Also sometimes we want to convert a date into some readable or presentable format. For example, we can rewrite the same date in multiple ways: "01.11.2021", "11/01/2021", "11 Nov, 2021", "2021-11-01", and so on... How can we transform dates so they still will be interpreted by Python?

So, how can we transform the date object into another format? At first, we need to load datetime class from the datetime library within a new line (yes, sounds tautological). Then, you need to apply .strftime() method of datetime class (datetime.strftime()) with a date as the first argument, and pattern as the second. There where the complexity starts. A pattern - is a string, that can be built using the next format codes (the list is not full):

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

Let's build some examples. For example, we can create a date object representing the 28th of June, 2012. And let's transform it into two forms: "June 28, 2012" and "2012/06/28". Consider the first format: there we need full month name (B), day of the month (%d) and 4-digit year (%Y). So the pattern will look like "%B %d, %Y". In a similar way the second pattern can be built: 4-digit year (%Y), month as a zero-padded decimal number (%m), and day of the month (%d). In this case, the pattern will look like "%Y/%m/%d".

1234567891011
# Load classes from library from datetime import date from datetime import datetime # Create date object some_date = date(2012, 6, 28) # Format date into the first format print("Transformed into the first format date:", datetime.strftime(some_date, "%B %d, %Y")) # Format date into the first format print("Transformed into the second format date:", datetime.strftime(some_date, "%Y/%m/%d"))
copy
Tarefa
test

Swipe to show code editor

Given date object (1998-03-11) assigned to variable day. Using .strftime() method format day to format: "11.03.98" and save the result in day_formatted variable.

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