Python – Get Month from year and weekday
Given a year and weekday, the task is to write a Python program to get a month and the week.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
Input : test_year = 1997, test_week = 27 Output : 1997-07-07 00:00:00 Explanation : 27th Week starts from 7 july in 1997. Input : test_year = 2021, test_week = 27 Output : 2021-07-05 00:00:00 Explanation : 27th Week starts from 5 july in 2021.
Method #1 : Using %W, %w
Parsing the date with %W sets us to start the week, additionally, %w gets the weekday required, we’ll use default to be 1. Can be tweaked as per requirements.
The original year, week is : 1997 27 The starting date of week : 1997-07-07 00:00:00
Method #2: Using relativedelta()
This just adds required weeks to the required year and gets the initial date required. This adds weeks, hence could give any weekday depending upon the start date of the year.
The original year, week is : 1997 27 The starting date of week : 1997-07-09