编程语言
首页 > 编程语言> > python doy与年月日转换

python doy与年月日转换

作者:互联网

1.年月日转DOY

def date2doy(year, month, day):
    month_leapyear = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    month_notleap = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    doy = 0

    if month == 1:
        pass
    elif year % 4 == 0 and (year % 100 != 0 or year % 400 == 0):
        for i in range(month - 1):
            doy += month_leapyear[i]
    else:
        for i in range(month - 1):
            doy += month_notleap[i]
    doy += day
    return doy

2.DOY转年月日

def doy2date(year,doy):    
       month_leapyear=[31,29,31,30,31,30,31,31,30,31,30,31]
       month_notleap= [31,28,31,30,31,30,31,31,30,31,30,31]

       if year%4==0 and (year%100!=0 or year%400==0):
          for i in range(0,12):
             if doy>month_leapyear[i]:
                 doy-=month_leapyear[i]
                 continue
             if doy<=month_leapyear[i]:
                 month=i+1
                 day=doy
                 break
       else:
          for i in range(0,12):
             if doy>month_notleap[i]:
                 doy-=month_notleap[i]
                 continue
             if doy<=month_notleap[i]:
                 month=i+1
                 day=doy
                 break
       return month,day

参考:https://blog.csdn.net/weixin_30748995/article/details/101570875

 

标签:python,31,30,month,notleap,doy,year,年月日
来源: https://www.cnblogs.com/19981127yjr/p/16394624.html