Course Content
Dealing with Dates and Times in Python
Dealing with Dates and Times in Python
Date Methods
There are also several methods available for date
objects. Let's consider some of them:
date.weekday()
- returns the day of week as an integer (0 is Monday, 6 is Sunday);date.isoweekday()
- returns the day of week as an integer (but this time 1 for Monday, and 7 for Sunday);date.replace(year = ..., month = ..., day = ...)
- replaces the specified arguments for givendate
object.
For example, using the date from the previous example (1st of November 2021) we can say what day of the week that was. Then, using .replace()
method we can change the year to 2022 and see what day of the week it will be. For a change, let's use both .weekday()
and .isoweekday()
methods.
# Load class from library from datetime import date # Create date object course_created = date(2021, 11, 1) # Print day of week for given date print(course_created.weekday()) # Replace year to 2022 next_year = course_created.replace(year = 2022) # Find out what day of week it will be print(next_year.isoweekday())
We got two numbers: 0 and 2. What do they mean? We got 0 from .weekday()
method which returns Monday as 0 and so on. So, the 1st of November 2021 was Monday. Then, we added to this date 1 year and .isoweekday()
returned 2. In this case that will be Tuesday, as .isoweekday()
method returns 1 for Monday and so on.
Now let's find out Halloween's days of the week.
Task
- Assign date October 31, 2021, to variable
halloween
. - Find out the day of the week for this date.
- Using
.replace
method create new variablehalloween_next
and assign the datehalloween
but with the next year (2022). - Find out the day of the week for
halloween_next
. Feel free to use either.weekday()
or.isoweekday()
methods.
Thanks for your feedback!
Date Methods
There are also several methods available for date
objects. Let's consider some of them:
date.weekday()
- returns the day of week as an integer (0 is Monday, 6 is Sunday);date.isoweekday()
- returns the day of week as an integer (but this time 1 for Monday, and 7 for Sunday);date.replace(year = ..., month = ..., day = ...)
- replaces the specified arguments for givendate
object.
For example, using the date from the previous example (1st of November 2021) we can say what day of the week that was. Then, using .replace()
method we can change the year to 2022 and see what day of the week it will be. For a change, let's use both .weekday()
and .isoweekday()
methods.
# Load class from library from datetime import date # Create date object course_created = date(2021, 11, 1) # Print day of week for given date print(course_created.weekday()) # Replace year to 2022 next_year = course_created.replace(year = 2022) # Find out what day of week it will be print(next_year.isoweekday())
We got two numbers: 0 and 2. What do they mean? We got 0 from .weekday()
method which returns Monday as 0 and so on. So, the 1st of November 2021 was Monday. Then, we added to this date 1 year and .isoweekday()
returned 2. In this case that will be Tuesday, as .isoweekday()
method returns 1 for Monday and so on.
Now let's find out Halloween's days of the week.
Task
- Assign date October 31, 2021, to variable
halloween
. - Find out the day of the week for this date.
- Using
.replace
method create new variablehalloween_next
and assign the datehalloween
but with the next year (2022). - Find out the day of the week for
halloween_next
. Feel free to use either.weekday()
or.isoweekday()
methods.
Thanks for your feedback!
Date Methods
There are also several methods available for date
objects. Let's consider some of them:
date.weekday()
- returns the day of week as an integer (0 is Monday, 6 is Sunday);date.isoweekday()
- returns the day of week as an integer (but this time 1 for Monday, and 7 for Sunday);date.replace(year = ..., month = ..., day = ...)
- replaces the specified arguments for givendate
object.
For example, using the date from the previous example (1st of November 2021) we can say what day of the week that was. Then, using .replace()
method we can change the year to 2022 and see what day of the week it will be. For a change, let's use both .weekday()
and .isoweekday()
methods.
# Load class from library from datetime import date # Create date object course_created = date(2021, 11, 1) # Print day of week for given date print(course_created.weekday()) # Replace year to 2022 next_year = course_created.replace(year = 2022) # Find out what day of week it will be print(next_year.isoweekday())
We got two numbers: 0 and 2. What do they mean? We got 0 from .weekday()
method which returns Monday as 0 and so on. So, the 1st of November 2021 was Monday. Then, we added to this date 1 year and .isoweekday()
returned 2. In this case that will be Tuesday, as .isoweekday()
method returns 1 for Monday and so on.
Now let's find out Halloween's days of the week.
Task
- Assign date October 31, 2021, to variable
halloween
. - Find out the day of the week for this date.
- Using
.replace
method create new variablehalloween_next
and assign the datehalloween
but with the next year (2022). - Find out the day of the week for
halloween_next
. Feel free to use either.weekday()
or.isoweekday()
methods.
Thanks for your feedback!
There are also several methods available for date
objects. Let's consider some of them:
date.weekday()
- returns the day of week as an integer (0 is Monday, 6 is Sunday);date.isoweekday()
- returns the day of week as an integer (but this time 1 for Monday, and 7 for Sunday);date.replace(year = ..., month = ..., day = ...)
- replaces the specified arguments for givendate
object.
For example, using the date from the previous example (1st of November 2021) we can say what day of the week that was. Then, using .replace()
method we can change the year to 2022 and see what day of the week it will be. For a change, let's use both .weekday()
and .isoweekday()
methods.
# Load class from library from datetime import date # Create date object course_created = date(2021, 11, 1) # Print day of week for given date print(course_created.weekday()) # Replace year to 2022 next_year = course_created.replace(year = 2022) # Find out what day of week it will be print(next_year.isoweekday())
We got two numbers: 0 and 2. What do they mean? We got 0 from .weekday()
method which returns Monday as 0 and so on. So, the 1st of November 2021 was Monday. Then, we added to this date 1 year and .isoweekday()
returned 2. In this case that will be Tuesday, as .isoweekday()
method returns 1 for Monday and so on.
Now let's find out Halloween's days of the week.
Task
- Assign date October 31, 2021, to variable
halloween
. - Find out the day of the week for this date.
- Using
.replace
method create new variablehalloween_next
and assign the datehalloween
but with the next year (2022). - Find out the day of the week for
halloween_next
. Feel free to use either.weekday()
or.isoweekday()
methods.