如何用Python找出今年6月16日(wk24)的周数?
当前回答
一般要获取当前周数(从周日开始):
from datetime import *
today = datetime.today()
print today.strftime("%U")
其他回答
有许多周编号系统。下面是最常见的系统代码示例:
ISO: First week starts with Monday and must contain the January 4th (or first Thursday of the year). The ISO calendar is already implemented in Python: >>> from datetime import date >>> date(2014, 12, 29).isocalendar()[:2] (2015, 1) North American: First week starts with Sunday and must contain the January 1st. The following code is my modified version of Python's ISO calendar implementation for the North American system: from datetime import date def week_from_date(date_object): date_ordinal = date_object.toordinal() year = date_object.year week = ((date_ordinal - _week1_start_ordinal(year)) // 7) + 1 if week >= 52: if date_ordinal >= _week1_start_ordinal(year + 1): year += 1 week = 1 return year, week def _week1_start_ordinal(year): jan1 = date(year, 1, 1) jan1_ordinal = jan1.toordinal() jan1_weekday = jan1.weekday() week1_start_ordinal = jan1_ordinal - ((jan1_weekday + 1) % 7) return week1_start_ordinal >>> from datetime import date >>> week_from_date(date(2014, 12, 29)) (2015, 1) MMWR (CDC): First week starts with Sunday and must contain the January 4th (or first Wednesday of the year). I created the epiweeks package specifically for this numbering system (also has support for the ISO system). Here is an example: >>> from datetime import date >>> from epiweeks import Week >>> Week.fromdate(date(2014, 12, 29)) (2014, 53)
我相信date. iscalendar()将是答案。本文解释了ISO 8601日历背后的数学原理。查看Python文档的datetime页面的date. iscalendar()部分。
>>> dt = datetime.date(2010, 6, 16)
>>> wk = dt.isocalendar()[1]
24
. iscalendar()返回一个三元组,包含(年,星期数,星期天数)。dt. iscalendar()[0]返回年份,dt. iscalendar()[1]返回周数,dt. iscalendar()[2]返回周数。尽可能的简单。
已经给出了很多答案,但我想补充一下。
如果您需要将周显示为年/周样式(例如,1953 - 2019年第53周,2001 - 2020年第1周等),您可以这样做:
import datetime
year = datetime.datetime.now()
week_num = datetime.date(year.year, year.month, year.day).strftime("%V")
long_week_num = str(year.year)[0:2] + str(week_num)
它将使用当前的年份和周,写这篇文章当天的long_week_num将是:
>>> 2006
iscalendar()对于某些日期返回错误的年和周数值:
Python 2.7.3 (default, Feb 27 2014, 19:58:35)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import datetime as dt
>>> myDateTime = dt.datetime.strptime("20141229T000000.000Z",'%Y%m%dT%H%M%S.%fZ')
>>> yr,weekNumber,weekDay = myDateTime.isocalendar()
>>> print "Year is " + str(yr) + ", weekNumber is " + str(weekNumber)
Year is 2015, weekNumber is 1
与Mark Ransom的方法相比:
>>> yr = myDateTime.year
>>> weekNumber = ((myDateTime - dt.datetime(yr,1,1)).days/7) + 1
>>> print "Year is " + str(yr) + ", weekNumber is " + str(weekNumber)
Year is 2014, weekNumber is 52
你可以尝试%W指令,如下所示:
d = datetime.datetime.strptime('2016-06-16','%Y-%m-%d')
print(datetime.datetime.strftime(d,'%W'))
'%W':以十进制数表示的一年中的周数(星期一作为一周的第一天)。新年中第一个星期一之前的所有日子都被认为是第0周。(00, 01,…53)
推荐文章
- 证书验证失败:无法获得本地颁发者证书
- 当使用pip3安装包时,“Python中的ssl模块不可用”
- 无法切换Python与pyenv
- Python if not == vs if !=
- 如何从scikit-learn决策树中提取决策规则?
- 为什么在Mac OS X v10.9 (Mavericks)的终端中apt-get功能不起作用?
- 将旋转的xtick标签与各自的xtick对齐
- 为什么元组可以包含可变项?
- 如何合并字典的字典?
- 如何创建类属性?
- 不区分大小写的“in”
- 在Python中获取迭代器中的元素个数
- 解析日期字符串并更改格式
- 使用try和。Python中的if
- 如何在Python中获得所有直接子目录