Python Forum
Convert calendarweek into date
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Convert calendarweek into date
#1
Hello there,

I am trying to convert a calendar week into the first and last day of that week.

So for example I am searching for the start and end date of the 24th calendar week of 2020 and the programm should tell me that the first day is the 8.06. and the last day of the week is 14.06.
Sadly the programs thinks that I mean the 24th of december 2019 and I dont know how to fix this.

My code looks as this:

from datetime import datetime, timedelta
date_y = "2020-"
date_w = "24"
date_str = date_y + date_w

date_obj = datetime.strptime(date_str, '%Y-%W')

# First day of the week
start_of_week = date_obj - timedelta(days=date_obj.weekday())  
# Last day of the week
end_of_week = start_of_week + timedelta(days=6)  

print(start_of_week)
print(end_of_week)
Reply
#2
Calculate the week from current datetime/date:
import datetime
year, week, day = datetime.datetime.now().isocalendar()
# weekday is from 1 - 7

# converting back to datetime/date
the_date = datetime.date.fromisocalendar(year, week, day)
the_datetime = datetime.datetime.fromisocalendar(year, week, day)
Now getting for date X the start date of the week and the end date of the week:

def get_week(date_or_datetime):
    MONDAY, SUNDAY = 1, 7
    calendar = datetime.date.fromisocalendar
    year, week, _ = date_or_datetime.isocalendar()
    return calendar(year, week, MONDAY), calendar(year, week, SUNDAY)
I assigned inside the function datetime.date.fromisocalendar to calendar.
Just to keep it shorter.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Compare current date on calendar with date format file name Fioravanti 1 118 Mar-26-2024, 08:23 AM
Last Post: Pedroski55
  Python date format changes to date & time 1418 4 515 Jan-20-2024, 04:45 AM
Last Post: 1418
  Review my code: convert a HTTP date header to a datetime object stevendaprano 1 1,905 Dec-17-2022, 12:24 AM
Last Post: snippsat
  Convert Date to another format lonesoac0 2 1,631 Mar-17-2022, 11:26 AM
Last Post: DeaD_EyE
  Date format and past date check function Turtle 5 4,068 Oct-22-2021, 09:45 PM
Last Post: deanhystad
  How to add previous date infront of every unique customer id's invoice date ur_enegmatic 1 2,190 Feb-06-2021, 10:48 PM
Last Post: eddywinch82
  Convert date integers (ex. 43831) to regular format Galven 2 2,552 Nov-15-2020, 11:38 PM
Last Post: bowlofred
  How to add date and years(integer) to get a date NG0824 4 2,803 Sep-03-2020, 02:25 PM
Last Post: NG0824
  Convert weekly sequences to date and time. SinPy 0 1,421 Nov-23-2019, 05:20 PM
Last Post: SinPy
  convert strings of date to datetime exported from CSV GiorgosPap31 1 3,947 Oct-31-2019, 02:37 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020