为什么python的datetime.isocalendar()和datetime.year不同?

2024-09-30 06:17:41 发布

您现在位置:Python中文网/ 问答频道 /正文

在Python2.6中尝试以下操作:

newyear_2012 = datetime.date.fromtimestamp(1325419200)
newyear.year # returns 2012
newyear.isocalendar() # return (2011, 52, 7)

怎么回事?这是虫子吗?我找不到文件,但也许我找的地方不对。

如果我转换unix时间戳here,它会告诉我:Sun,2012年1月1日12:00:00 GMT

更新: 下面的答案解释了这不是python错误,而是由于ISO规范。如果你想得到一周的数字,而这个数字永远不会把一年的前几天放在第52周,你可以尝试以下方法:

week_no = int(time.strftime("%U", datetime_object.timetuple()))

我不确定它对应的是什么标准,但从我的角度来看,它的行为更直观(在我的申请周数中,一年内的周数应该随着时间的推移而上升)。


Tags: 文件datetimedatereturn地方时间unix数字

热门问题