Skip to content
Fix Code Error

How do I get the day of week given a date?

March 13, 2021 by Code Error
Posted By: Anonymous

I want to find out the following:
given a date (datetime object), what is the corresponding day of the week?

For instance, Sunday is the first day, Monday: second day.. and so on

And then if the input is something like today’s date.

Example

>>> today = datetime.datetime(2017, 10, 20)
>>> today.get_weekday()  # what I look for

The output is maybe 6 (since it’s Friday)

Solution

Use weekday():

>>> import datetime
>>> datetime.datetime.today()
datetime.datetime(2012, 3, 23, 23, 24, 55, 173504)
>>> datetime.datetime.today().weekday()
4

From the documentation:

Return the day of the week as an integer, where Monday is 0 and Sunday is 6.

Answered By: Anonymous

Related Articles

  • Is CSS Turing complete?
  • Javascript and css animation
  • Get first day of week in SQL Server
  • Calculate the number of business days between two dates?
  • Get the correct week number of a given date
  • Make datetime derived from one column
  • How to get min max day in a week every month?
  • Convert Python dict into a dataframe
  • Dodged bar plot in R based on to columns with count Year…
  • Get day of week using NSDate

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

How to replace a character by a newline in Vim

Next Post:

java : convert float to String and String to float

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Get code errors & solutions at akashmittal.com
© 2022 Fix Code Error