我在用 gmtime, strftime and datetime.
为了提供上下文,我正在设置一个呼叫计划,并允许人们轻松地切换呼叫计划,我编写了一个脚本来更改xml文件以完成此任务。为了让通话中的每个人都更轻松一些,我设置了脚本,以便您可以询问特定日期的星期几。大多数内置功能都将星期日或星期一作为开始日期,而这不是我所需要的。
有问题的功能:
我知道%U
从周日开始,我只需要解决一下问题,这样它就会计算出从周三开始的时间。
`#option 2
def getWeek():
print("\n\n")
date = input("Enter the date you want the week number of (MM DD YYYY): ")
if(len(date) != 10):
print("You typed it in wrong. Try it again.")
getWeek()
else:
d = time.strptime(date, "%m %d %Y")
print(strftime("%U" + 2, d))`
您为什么不自己手动计算周数。
基本算法:
在代码中:
>>> wednesday = 3 # sunday = 0, monday = 1, etc.
>>> week_no = lambda d: (d.tm_yday - (d.tm_wday-wednesday)%7) // 7 + 1
>>> week_no(time.strptime("10 06 2016", "%m %d %Y")) # Thursday
41
>>> week_no(time.strptime("10 04 2016", "%m %d %Y")) # Tuesday
40
完整性检查:
>>> time.strftime("%U", time.strptime("10 06 2016", "%m %d %Y"))
'40'
>>> time.strftime("%U", time.strptime("10 04 2016", "%m %d %Y"))
'40'
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句