Jay
Jay

Reputation: 31

Week number to week commencing date USING PYTHON?

n=10

I want to get the week commencing date of the 10th week of the current year (2022), i.e., 3/7/2022. How can this be done using datetime functions?

Upvotes: 1

Views: 37

Answers (1)

Cow
Cow

Reputation: 3030

You need to use the %W directive, but you also need to specify what the start day is of the week and of course the year.

Example:

from datetime import datetime
print(datetime.strptime("10-2022-1", "%W-%Y-%w"))

Result:

2022-03-07 00:00:00
  1. 10 is the week number
  2. 2022 is the year
  3. 1 is the day of the week to start with (Monday) in order to get an actual date.

Datetime formats: https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes

Upvotes: 1

Related Questions