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 givendateobject.
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.
123456789101112# 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.
Swipe to start coding
- Assign date October 31, 2021, to variable
halloween. - Find out the day of the week for this date.
- Using
.replacemethod create new variablehalloween_nextand assign the datehalloweenbut with the next year (2022). - Find out the day of the week for
halloween_next. Feel free to use either.weekday()or.isoweekday()methods.
Løsning
Tak for dine kommentarer!
single
Spørg AI
Spørg AI
Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat
Awesome!
Completion rate improved to 3.23
Date Methods
Stryg for at vise menuen
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 givendateobject.
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.
123456789101112# 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.
Swipe to start coding
- Assign date October 31, 2021, to variable
halloween. - Find out the day of the week for this date.
- Using
.replacemethod create new variablehalloween_nextand assign the datehalloweenbut with the next year (2022). - Find out the day of the week for
halloween_next. Feel free to use either.weekday()or.isoweekday()methods.
Løsning
Tak for dine kommentarer!
single