Date and time from year and week number

I have a year and a week number that I want to convert to an object datetime.datetiem. My (naive?) Reading of the documentation suggested that I strptime('2016 00', '%Y %W')should do just that. However:

In [2]: from datetime import datetime

In [3]: datetime.strptime('2016 00', '%Y %W')
Out[3]: datetime(2016, 1, 1, 0, 0)

In [4]: datetime.strptime('2016 52', '%Y %W')
Out[4]: datetime(2016, 1, 1, 0, 0)

What am I doing wrong?

+4
source share
2 answers

So, it turns out that the week number is strptimenot enough to get the date. Add the default day of the week to the row for it to work.

> from datetime import datetime
> myDate = "2016 51"
> datetime.strptime(myDate + ' 0', "%Y %W %w")
> datetime.datetime(2016, 12, 25, 0, 0)

0 reports that he selects the Sunday of that week, but you can change this between 0 and 6 for each day.

+3
source

From the docs (see note 7 below):

strptime() %U %W , .

, , , datetime.strptime('2016', '%Y').

+2

Source: https://habr.com/ru/post/1648793/


All Articles