Home > Backend Development > Python Tutorial > How Can I Determine the Day of the Week for a Given Date in Python?

How Can I Determine the Day of the Week for a Given Date in Python?

Mary-Kate Olsen
Release: 2024-12-05 14:17:12
Original
200 people have browsed it

How Can I Determine the Day of the Week for a Given Date in Python?

Determine the Day of Week from a Date

In cases where you need to retrieve the corresponding day of the week from a given date, Python provides a convenient solution.

Solution Using weekday():

  1. Import the datetime module:

    import datetime
    Copy after login
  2. Create a datetime object representing the given date:

    import datetime
    today = datetime.datetime(2017, 10, 20)
    Copy after login
  3. Retrieve the day of the week using the weekday() method:

    day_of_week = today.weekday()
    Copy after login

This method returns an integer value representing the day of the week, with Monday as 0 and Sunday as 6. For example, if the input date corresponds to Friday, day_of_week will be 6.

The above is the detailed content of How Can I Determine the Day of the Week for a Given Date in Python?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template