Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Date Methods | Working with Dates
Dealing with Dates and Times in Python

book
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 given date 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())
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())
copy

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.

Oppgave

Swipe to start coding

  1. Assign date October 31, 2021, to variable halloween.
  2. Find out the day of the week for this date.
  3. Using .replace method create new variable halloween_next and assign the date halloween but with the next year (2022).
  4. Find out the day of the week for halloween_next. Feel free to use either .weekday() or .isoweekday() methods.

Løsning

# Load class from library
from datetime import date

# Create date object
halloween = date(2021, 10, 31)

# Find the day of week of halloween
print(halloween.weekday())

# Create variable halloween_next with 2022 year
halloween_next = halloween.replace(year = 2022)

# Find out day of week for halloween_next
print(halloween_next.isoweekday())

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 4
# Load class from library
from datetime import date

# Create date object
halloween = date(___)

# Find the day of week of halloween
print(halloween.___())

# Create variable halloween_next with 2022 year
halloween_next = halloween.___(year = ___)

# Find out day of week for halloween_next
print(___.___())
toggle bottom row
some-alt