如何在Python中从星期日开始的星期找到星期数? [英] How can I find week numbers with weeks starting on Sunday in Python?

查看:142
本文介绍了如何在Python中从星期日开始的星期找到星期数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python的新手总数,如果很明显,请提前道歉。我有一些日期时间值,例如,我想为其指定年和周的数字:

Total newbie to Python, so apologies in advance if this is obvious. I have a number of datetime values, for which I would like to identify a year and week number, for example:

start_date = datetime.datetime(2015,1,26,0,0,0)

我要抓住的是我希望将周定义为从星期日开始。

The catch is, I would like the week to be defined as starting on a Sunday.

我知道datetime.isocalendar()可以获取我的周号,但是它们将根据假设每周从星期一开始。还有其他方法吗?

I understand that the datetime.isocalendar() can get me week numbers, but they will be calculated under the assumption that weeks start on Mondays. Is there any other way?

编辑:运行Python 2.7.6

Running Python 2.7.6

谢谢!

推荐答案

字符串格式%U 将给出从星期日算起的一年中的星期:

String format %U will give the week of the year counting from Sunday:

t1 = datetime.datetime.now()
t1.strftime("%U")

请参见 http:// strftime .org / 获取格式参数的完整列表。

See http://strftime.org/ for full list of format parameters.

这篇关于如何在Python中从星期日开始的星期找到星期数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆