#给出一个日期和一个数字,判断日期加上数字之后的新日期

 

def day(date,num):
    #date=raw_input("(MM/DD/YY)>>")
    date=date.split("/")
    year1=int(date[-1])
    month1=int(date[0])
    day1=int(date[1])
    #num=raw_input("enter a number:")
    num=int(num)
    month_days=[31,28,31,30,31,30,31,31,30,31,30,31]
    
    if (int(date[2])%4==0 and int(date[2])%100!=0) or int(date[2])%400==0:
        month_days[1]=29
        years1=366
    else:
        years1=365
    days1=sum(month_days[:(month1-1)])+day1
    sumdays=days1+num
    if num>=0: #数字为正数或者0
        lis=[]
        if sumdays<=years1: #本年
            year2=year1
            print year2
            lis.append(year2)
            sumday=0
            for i,j in enumerate(month_days):
                if sumday<sumdays:
                    sumday=sumday+int(j)
                
            month2=i+1
            print month2
            lis.append(month2)
            marginday=sum(month_days[:i+1])-sumdays
            day2=month_days[i]-marginday
            print day2
            lis.append(day2)
             
            print "%d天后的日期为:%d年%d月%d号"%(num,lis[0],lis[1],lis[2])   
        else:  #不在本年

            lis=[]
            remain1=years1-days1
            remain2=num-remain1
            
            year2=year1+1
            if (year2%4==0 and year2%100!=0) or year2%400==0:
                years2=366
                month_days[1]=29   
            else:
                years2=365
                month_days[1]=28 
                
            while remain2>years2:              
                remain2=remain2-years2
                year2=year2+1
                if (year2%4==0 and year2%100!=0) or year2%400==0:
                    years2=366
                    month_days[1]=29   
                else:
                    years2=365 
                    month_days[1]=28
            lis.append(year2)
                   
           
            for i,j in enumerate(month_days):
                if remain2<=sum(month_days[:i+1]):
                    month2=i+1
                    lis.append(month2)
                    day2=remain2-sum(month_days[:i])
                    lis.append(day2)
                    break
            print type(lis)
            print lis
            print "%d天后的日期为:%d年%d月%d号"%(num,lis[0],lis[1],lis[2])
    else: #num为负数的时候
        lis=[]
        remindays=0
        years2=years1
        year2=year1        
        if sumdays<=0:
            remindays=abs(sumdays)
            while remindays>=0:
                year2=year2-1
                
                if (year2%4==0 and year2%100!=0) or year2%400==0:
                    remindays=remindays-366
                    month_days[1]=29
                else:
                    remindays=remindays-365
                    month_days[1]=28
            lis.append(year2)
            print remindays  
            remindays=abs(remindays)
        else:
            remindays=sumdays
            lis.append(year1)
            
        print remindays
        for i,j in enumerate(month_days):
            if remindays<=sum(month_days[:i+1]):
                month2=i+1
                lis.append(month2)
                day2=remindays-sum(month_days[:i])
                lis.append(day2)
                break
            
        print "%d天后的日期为:%d年%d月%d号"%(num,lis[0],lis[1],lis[2])   
        
day("3/1/2016",'365')    

 

posted on 2017-06-30 11:04  竹子的梦想在路上  阅读(221)  评论(0编辑  收藏  举报