python – 按周,日和年逐月获取
发布时间:2020-11-14 05:49:22 所属栏目:Python 来源:互联网
导读:我想知道你如何通过给予日,周数和年份来获得月份. 例如,如果你有这样的东西 def getmonth(day, week, year): # by day, week and year calculate the month print (month)getmonth(28, 52, 2014)# print 12getmonth(13, 42, 2014)# prin
|
我想知道你如何通过给予日,周数和年份来获得月份. 例如,如果你有这样的东西 def getmonth(day,week,year):
# by day,week and year calculate the month
print (month)
getmonth(28,52,2014)
# print 12
getmonth(13,42,2014)
# print 10
getmonth(6,2,2015)
# print 1
解决方法每 interjay’s suggestion:import datetime as DT
def getmonth(day,year):
for month in range(1,13):
try:
date = DT.datetime(year,month,day)
except ValueError:
continue
iso_year,iso_weeknum,iso_weekday = date.isocalendar()
if iso_weeknum == week:
return date.month
print(getmonth(28,2014))
# 12
print(getmonth(13,2014))
# 10
print(getmonth(6,2015))
# 1 (编辑:长春站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
- python – 当涉及离散变量时,pymc3与pymc2的困难
- python中is和==的区别
- python – 将所有STDIN“啜饮”成字符串的最有效
- import pyttsx在python 2.7中工作,但不在python3
- python-2.7 – 在Python 2.7中手动构建ConfigPar
- python – 错误“没有提供异常”是什么意思?
- python – Django Test Client post()返回302,尽
- python – “I; 16”图像文件的numpy.array
- Python HTMLParser将数据划分为
- python – LinkExtractor和SgmlLinkExtractor之间
热点阅读
