Python 从周数获取日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17087314/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:24:18  来源:igfitidea点击:

Get date from week number

pythondatetimestrptime

提问by Ali SAID OMAR

Please what's wrong with my code:

请问我的代码有什么问题:

import datetime
d = "2013-W26"
r = datetime.datetime.strptime(d, "%Y-W%W")
print(r)

Display "2013-01-01 00:00:00", Thanks.

显示“2013-01-01 00:00:00”,谢谢。

采纳答案by Martijn Pieters

A week number is not enough to generate a date; you need a day of the week as well. Add a default:

周数不足以生成日期;你也需要一周中的一天。添加默认值:

import datetime
d = "2013-W26"
r = datetime.datetime.strptime(d + '-1', "%Y-W%W-%w")
print(r)

The -1and -%wpattern tells the parser to pick the Monday in that week. This outputs:

-1-%w模式告诉解析器挑周一在那一周。这输出:

2013-07-01 00:00:00

%Wuses Monday as the first day of the week. While you can pick your own weekday, you may get unexpected results if you deviate from that.

%W使用星期一作为一周的第一天。虽然您可以选择自己的工作日,但如果您偏离了它,您可能会得到意想不到的结果。

See the strftime()and strptime()behavioursection in the documentation, footnote 4:

请参阅文档中的strftime()strptime()行为部分,脚注 4:

When used with the strptime()method, %Uand %Ware only used in calculations when the day of the week and the year are specified.

当与使用strptime()方法,%U%W指定了一周,一年中的一天,在计算仅使用。

Note, if your week number is a ISO week date, you'll want to use %G-W%V-%uinstead! Those directives require Python 3.6 or newer.

请注意,如果您的周数是ISO 周日期,则您需要%G-W%V-%u改用!这些指令需要 Python 3.6 或更新版本。

回答by Ron

Here's a handy functionincluding the issue with zero-week.

这是一个方便的功能,包括零周问题。

回答by Senthilkumar C

import datetime
res = datetime.datetime.strptime("2018 W30 w1", "%Y %W w%w")
print res

Adding of 1 as week day will yield exact current week start. Adding of timedelta(days=6) will gives you the week end.

添加 1 作为工作日将产生准确的当前周开始。添加 timedelta(days=6) 将为您提供周末。

datetime.datetime(2018, 7, 23)

回答by Luka Kikelj

To complete the other answers - if you are using ISOweek numbers, this string is appropriate (to get the Monday of a given ISO week number):

要完成其他答案 - 如果您使用ISO周数,则此字符串是合适的(获取给定 ISO 周数的星期一):

import datetime
d = '2013-W26'
r = datetime.datetime.strptime(d + '-1', '%G-W%V-%u')
print(r)

%G, %V, %uare ISO equivalents of %Y, %W, %w, so this outputs:

%G%V%u是ISO当量%Y%W%w,所以这个输出:

2013-06-24 00:00:00

Availabe in Python 3.6+; from docs.

在 Python 3.6+ 中可用;来自文档

回答by dimosbele

In case you have the yearly number of week, just add the number of weeks to the first day of the year.

如果您有每年的周数,只需将周数添加到一年的第一天。

>>> import datetime
>>> from dateutil.relativedelta import relativedelta

>>> week = 40
>>> year = 2019
>>> date = datetime.date(year,1,1)+relativedelta(weeks=+week)
>>> date
datetime.date(2019, 10, 8)

回答by Jens W. Klein

In Python 3.8 there is the handy datetime.date.fromisocalendar:

在 Python 3.8 中有一个方便的datetime.date.fromisocalendar

>>> from datetime import date
>>> date.fromisocalendar(2020, 1, 1)  # (year, week, day of week)
datetime.date(2019, 12, 30, 0, 0)

In older Python versions (3.7-) the calculation can use the information from datetime.date.isocalendarto figure out the week ISO8601 compliant weeks:

在较旧的 Python 版本 (3.7-) 中,计算可以使用信息datetime.date.isocalendar来计算 符合 ISO8601 的周数:

from datetime import date, timedelta

def monday_of_calenderweek(year, week):
    first = date(year, 1, 1)
    base = 1 if first.isocalendar()[1] == 1 else 8
    return first + timedelta(days=base - first.isocalendar()[2] + 7 * (week - 1))

Both works also with datetime.datetime.

两者都适用于datetime.datetime.