python 获取日期所在周一及周日_在Python中使用日期获取星期数

在Python中,对于日期datetime.date(2013, 12, 30),使用.isocalendar()[1]获取的是一年的第1周。这是因为ISO8601标准规定,一周的开始是周一,且包含该年第一周的周四。如果要获取当年的最后一周,可以使用12月28日,因为它总是处于当年的最后一周。" 113517390,7741420,Python爬取CSDN账号文章并存储到MySQL,"['爬虫', 'Python', '数据库管理', '数据抓取', 'CSDN']
摘要由CSDN通过智能技术生成

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 ISO 8601 definition for week 01 is the week with the year's first Thursday in it.

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

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

It is the first week with a majority (four or more) of its days in January (ISO weeks start on Monday)

Its first day is the Monday nearest to 1 January.

It has 4 January in it. Hence the earliest possible dates are 29 December through 4 January, the latest 4 through 10 January.

It has the year's first working day in it, if Saturdays, Sundays and 1 January are not working days.

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]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值