获取第几周开始的日期

2018-01-08  本文已影响0人  Irlans
#weekflag格式为201435
def getfirstday(weekflag):
     
    yearnum = weekflag[0:4]   #取到年份
    weeknum = weekflag[4:6]   #取到周
    stryearstart = yearnum +'0101'   #当年第一天
    yearstart = datetime.datetime.strptime(stryearstart,'%Y%m%d') #格式化为日期格式
    yearstartcalendarmsg = yearstart.isocalendar()  #当年第一天的周信息
    yearstartweek = yearstartcalendarmsg[1]  
    yearstartweekday = yearstartcalendarmsg[2]
    yearstartyear = yearstartcalendarmsg[0]
    if yearstartyear < int (yearnum):
        daydelat = (8-int(yearstartweekday))+(int(weeknum)-1)*7
    else :
        daydelat = (8-int(yearstartweekday))+(int(weeknum)-2)*7
     
    a = (yearstart+datetime.timedelta(days=daydelat)).date()
    return a
    #输出2014年第35周的开始时间
上一篇下一篇

猜你喜欢

热点阅读