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

Course Content

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

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.

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.

Task

  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.

Task

  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.

Switch to desktop for real-world practiceContinue from where you are using one of the options below

Everything was clear?

Section 1. Chapter 4
toggle bottom row

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.

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.

Task

  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.

Task

  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.

Switch to desktop for real-world practiceContinue from where you are using one of the options below

Everything was clear?

Section 1. Chapter 4
toggle bottom row

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.

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.

Task

  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.

Task

  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.

Switch to desktop for real-world practiceContinue from where you are using one of the options below

Everything was clear?

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.

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.

Task

  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.

Switch to desktop for real-world practiceContinue from where you are using one of the options below
Section 1. Chapter 4
Switch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt