首页 > 试题广场 >

日期差值

[编程题]日期差值
  • 热度指数:43248 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
有两个日期,求两个日期之间的天数,如果两个日期是连续的我们规定他们之间的天数为两天

输入描述:
有多组数据,每组数据有两行,分别表示两个日期,形式为YYYYMMDD


输出描述:
每组数据输出一行,即日期差值
示例1

输入

20110412
20110422

输出

11
import datetime
def main():
    time1 = input()
    time2 = input()
    time1 = datetime.date(int(time1[0:4]), int(time1[4:6]), int(time1[6:]))
    time2 = datetime.date(int(time2[0:4]), int(time2[4:6]), int(time2[6:]))

    det = abs((time2-time1).days)
    print(det+1)

if __name__ == '__main__':
    main()
编辑于 2021-01-23 12:48:29 回复(0)
while True:
    try:
        inp1=list(input().strip())
        inp2=list(input().strip())
        list1=[31,28,31,30,31,30,31,31,30,31,30,31]
        list2=[31,29,31,30,31,30,31,31,30,31,30,31]
        min_num=min(int(''.join(inp1)),int(''.join(inp2)))
        max_num=max(int(''.join(inp1)),int(''.join(inp2)))
        def isrunnian(i):
            if i%100!=0 and i%4==0:
                return True
            elif i%400==0:
                return True
            else:
                return False
        def data(i,info):
            result=0
            month=int(i[4:6])
            day=int(i[6:])
            if info:
                result+=sum(list2[:month-1])
                result+=day
            else:
                result+=sum(list1[:month-1])
                result+=day
            return result
        if isrunnian(int(str(min_num)[0:4])):
            info1=True
        else:
            info1=False
        if isrunnian(int(str(max_num)[0:4])):
            info2=True
        else:
            info2=False
        sum1=data(str(min_num),info1)
        sum2=data(str(max_num),info2)
        min_year=int((str(min_num)[0:4]))
        max_year=int((str(max_num)[0:4]))
        for i in range(min_year,max_year):
            if isrunnian(i):
                sum2+=366
            else:
                sum2+=365
        print(sum2-sum1+1)

    except:
        break
发表于 2019-08-20 10:00:57 回复(0)

分别对年月日进行差值计算累加天数

#coding : utf-8

year = [365, 366]
month = [[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],[31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]]
def isRun(y):
    if y%400==0 or (y%100!=0 and y%4==0):
        return 1
    return 0
def count(y1, m1, d1, y2, m2, d2):
    cnt = 0
    for y in xrange(y1, y2):
        cnt+=year[isRun(y)]
    y2_isRun = isRun(y2)
    for m in xrange(m1, m2):
        cnt+=month[y2_isRun][m-1]
    return cnt+d2-d1+1
while True:
    try:
        date1 = raw_input().strip()
        date2 = raw_input().strip()
        y1 = int(date1[0:4])
        y2 = int(date2[0:4])
        m1 = int(date1[4:6])
        m2 = int(date2[4:6])
        d1 = int(date1[6:8])
        d2 = int(date2[6:8])
        if(y1>y2) or (y1==y2 and m1>m2) or (y1==y2 and m1==m2 and d1>d2):
            y1 = int(date2[0:4])
            y2 = int(date1[0:4])
            m1 = int(date2[4:6])
            m2 = int(date1[4:6])
            d1 = int(date2[6:8])
            d2 = int(date1[6:8])
        print count(y1, m1, d1, y2, m2, d2)
    except:
        break
发表于 2019-01-07 11:24:37 回复(0)
import datetime
while True:
    try:
        date1 = input()
        date2 = input()
        print((datetime.datetime(int(date2[:4]),int(date2[4:6]),int(date2[6:]))-datetime.datetime(int(date1[:4]),int(date1[4:6]),int(date1[6:]))).days+1)
    except Exception:
        break
编辑于 2018-10-13 14:18:07 回复(0)

python只需要一行代码搞定。

要借助datetime库。

from datetime import datetime
while True:
    try:
        print(-(datetime.strptime(input(),"%Y%m%d")-datetime.strptime(input(),"%Y%m%d")).days+1)
    except:
        break
编辑于 2018-01-27 16:12:49 回复(3)