What i want is first date of 3rd week in January, 2021.
Provided that your string is of the format 2021-m1-w3
you could first parse the year, month ad week and then you can create a datetime.datetime
that holds the first day of the month of the year you want (of the data you parsed) and you can add a datetime.timedelta(weeks=weeks)
to it.
Parsing it is quite straightforward, you just have to split -
and remove the m
and w
characters.
year, month, weeks = [int(item.replace("m", "").replace("w", "")) for item in str_date.split("-")]
Then you can create the object and add it with the weeks
DATE = datetime.datetime(year=year, month=month,day=1)
DATE + datetime.timedelta(weeks=weeks-1) # -1 because it is the start
The output is datetime.datetime(2021, 1, 15, 0, 0)
which is the first day of week 3 of January 2021