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
Mary-Kate OlsenOriginal
2024-12-05 14:17:12252browse

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
  2. Create a datetime object representing the given date:

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

    day_of_week = today.weekday()

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!

Statement:
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