0

I'm writing a script where the user needs to input a week number and a procedure will be ran based on that. However, I ran into a small issue, I know I can get week numbers via something like this:

>>> a=datetime.datetime.now()
>>> a
datetime.datetime(2015, 1, 22, 15, 51, 57, 820058)
>>> a.isocalendar()[1]
4

But I can't find how to do it backwards. Also, the date I require has to be Sunday of that week at 6:00am. Once I have that datetime element I can just do

begin_date = datetime.datetime.strptime(a, "%Y-%m-%d %H:%M:%S")

To get the format I want. I'm still missing the step to get the date. Any thoughts?

rodrigocf
  • 1,951
  • 13
  • 39
  • 62

2 Answers2

2

We create an initial datetime of for 2015 (2014-12-28 6:00:00 --1st sunday of 1st week), and a timedelta object of 7 days:

w0 = datetime.datetime(2014,12,28,6)
d7 = datetime.timedelta(7)

Then you can simply add multiples of the timedelta object to the initial date like so (n would be your week number):

w0+(d7*n)
derks
  • 546
  • 1
  • 4
  • 7
0
>>> now = datetime.datetime.now()
>>> start = datetime.datetime(2015, 1,1)
>>> (now - start).days // 7
3
denz
  • 386
  • 1
  • 6