本文實例講述了Python計算兩個日期相差天數的方法。分享給大家供大家參考,具體如下:
#!/usr/bin/pythonimport timeimport sysdef dateinput():    date = raw_input('please input the first date: ')    return datedef datetrans(tdate):    spdate = tdate.replace("/","-")    try:        datesec = time.strptime(spdate,'%Y-%m-%d')    except ValueError:        print "%s is not a rightful date!!" % tdate        sys.exit(1)    return time.mktime(datesec)def daysdiff(d1,d2):    daysec = 24 * 60 * 60    return int(( d1 - d2 )/daysec)date1 = dateinput()date2 = dateinput()date1sec = datetrans(date1)date2sec = datetrans(date2)print "The number of days between two dates is: ",daysdiff(date1sec,date2sec)PS:這里再為大家推薦幾款關于日期與天數計算的在線工具供大家使用:
在線日期/天數計算器:
http://tools.jb51.net/jisuanqi/date_jisuanqi
在線萬年歷日歷:
http://tools.jb51.net/bianmin/wannianli
在線陰歷/陽歷轉換工具:
http://tools.jb51.net/bianmin/yinli2yangli
更多關于Python相關內容感興趣的讀者可查看本站專題:《Python日期與時間操作技巧總結》、《Python URL操作技巧總結》、《Python圖片操作技巧總結》、《Python數據結構與算法教程》、《Python Socket編程技巧總結》、《Python函數使用技巧總結》、《Python字符串操作技巧匯總》、《Python入門與進階經典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設計有所幫助。
新聞熱點
疑難解答