本文介绍了在Python中使用日期获取星期数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

日期是 datetime.date(2013,12,30)



我正在尝试获取周数使用

 导入datetime 
datetime.date(2013,12,30).isocalendar()[1]

我的输出为,

  1 

为什么我没有得到去年的星期数,而是我



我在这里做什么错了?

解决方案

您没有做错任何事,根据:

该周的星期四是2014/01/02。 p>

其他解释定义的方法,来自同一链接的Wiki Pedia文章:

如果您要查找给定年份的最后一周(52或53) ,具体取决于年份),我会使用12月28日,因为这总是保证在最后一周(因为1月4日始终是明年的第一周的一部分):

  def lastweeknumber(year):
返回datetime.date(year,12,28).isocalendar()[1]


Date is datetime.date(2013, 12, 30)

I am trying to get week number using

import datetime
datetime.date(2013, 12, 30).isocalendar()[1]

I am getting output as ,

1

Why i am not getting week number of last year , instead i am getting week number of current year?

Whats wrong i am doing here ?

解决方案

You are doing nothing wrong, 2013/12/30 falls in week 1 of 2014, according to the ISO8601 week numbering standard:

The Thursday in that week is 2014/01/02.

Other ways to explain the definition, from the same linked WikiPedia article:

If you were looking for the last week number of a given year (52 or 53, depending on the year), I'd use December 28th, which is always guaranteed to be in the last week (because January 4th is always part of the first week of the next year):

def lastweeknumber(year):
    return datetime.date(year, 12, 28).isocalendar()[1]

这篇关于在Python中使用日期获取星期数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 19:30
查看更多