Course Content
Dealing with Dates and Times in Python
Dealing with Dates and Times in Python
Date 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 code | Meaning | Example |
%d | Day of the month as a zero-padded decimal number | 01, 02, ..., 30, 31 |
%m | Month as a zero-padded decimal number | 01, 02, ..., 11, 12 |
%y | Year without century as a zero-padded decimal number | 00, 01, 02, ..., 98, 99 |
%Y | Year with century as a decimal number | 0001, 0002, 1999, 2000, 2001, ... |
%b | Month as locale’s abbreviated name | Jan, Feb, ..., Nov, Dec |
%B | Month as locale’s full name | January, February, ..., November, December |
%a | Weekday as locale’s abbreviated name | Sun, Mon, ..., Fri, Sat |
%A | Weekday as locale’s full name | Sunday, 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"
.
# 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"))
Task
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.
Thanks for your feedback!
Date 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 code | Meaning | Example |
%d | Day of the month as a zero-padded decimal number | 01, 02, ..., 30, 31 |
%m | Month as a zero-padded decimal number | 01, 02, ..., 11, 12 |
%y | Year without century as a zero-padded decimal number | 00, 01, 02, ..., 98, 99 |
%Y | Year with century as a decimal number | 0001, 0002, 1999, 2000, 2001, ... |
%b | Month as locale’s abbreviated name | Jan, Feb, ..., Nov, Dec |
%B | Month as locale’s full name | January, February, ..., November, December |
%a | Weekday as locale’s abbreviated name | Sun, Mon, ..., Fri, Sat |
%A | Weekday as locale’s full name | Sunday, 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"
.
# 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"))
Task
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.
Thanks for your feedback!
Date 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 code | Meaning | Example |
%d | Day of the month as a zero-padded decimal number | 01, 02, ..., 30, 31 |
%m | Month as a zero-padded decimal number | 01, 02, ..., 11, 12 |
%y | Year without century as a zero-padded decimal number | 00, 01, 02, ..., 98, 99 |
%Y | Year with century as a decimal number | 0001, 0002, 1999, 2000, 2001, ... |
%b | Month as locale’s abbreviated name | Jan, Feb, ..., Nov, Dec |
%B | Month as locale’s full name | January, February, ..., November, December |
%a | Weekday as locale’s abbreviated name | Sun, Mon, ..., Fri, Sat |
%A | Weekday as locale’s full name | Sunday, 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"
.
# 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"))
Task
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.
Thanks for your 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 code | Meaning | Example |
%d | Day of the month as a zero-padded decimal number | 01, 02, ..., 30, 31 |
%m | Month as a zero-padded decimal number | 01, 02, ..., 11, 12 |
%y | Year without century as a zero-padded decimal number | 00, 01, 02, ..., 98, 99 |
%Y | Year with century as a decimal number | 0001, 0002, 1999, 2000, 2001, ... |
%b | Month as locale’s abbreviated name | Jan, Feb, ..., Nov, Dec |
%B | Month as locale’s full name | January, February, ..., November, December |
%a | Weekday as locale’s abbreviated name | Sun, Mon, ..., Fri, Sat |
%A | Weekday as locale’s full name | Sunday, 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"
.
# 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"))
Task
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.