国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python實現計算兩個時間之間相差天數的方法

2020-02-16 01:31:24
字體:
來源:轉載
供稿:網友

本文實例講述了Python實現計算兩個時間之間相差天數的方法。分享給大家供大家參考,具體如下:

#-*- encoding:UTF-8 -*-from datetime import dateimport timenowtime = date.today()def convertstringtodate(stringtime):  "把字符串類型轉換為date類型"  if stringtime[0:2] == "20":    year=stringtime[0:4]    month=stringtime[4:6]    day=stringtime[6:8]    begintime=date(int(year),int(month),int(day))    return begintime  else :    year="20"+stringtime[0:2]    month=stringtime[2:4]    day=stringtime[4:6]    begintime=date(int(year),int(month),int(day))    return begintimedef comparetime(nowtime,stringtime):  "比較兩個時間,并返回兩個日期之間相差的天數"  if isinstance(nowtime,date):    pass  else:    nowtime=convertstringtodate(nowtime)  if isinstance(stringtime,date):    pass  else:    stringtime=convertstringtodate(stringtime)  result=nowtime-stringtime  return result.days"""  if stringtime[0:2] == "20":    year=stringtime[0:4]    month=stringtime[4:6]    day=stringtime[6:8]    begintime=date(int(year),int(month),int(day))    endtime=nowtime    result=endtime-begintime    return result.days  else :    year="20"+stringtime[0:2]    month=stringtime[2:4]    day=stringtime[4:6]    begintime=date(int(year),int(month),int(day))    endtime=nowtime    result=endtime-begintime    return result.days"""print isinstance("20141012",date)print comparetime(nowtime,"140619")

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程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 老河口市| 罗田县| 宁乡县| 山东省| 舒城县| 子洲县| 来宾市| 抚顺市| 炎陵县| 扶风县| 任丘市| 伊川县| 惠东县| 思茅市| 普安县| 泾川县| 东方市| 肇源县| 柯坪县| 宁明县| 黑河市| 靖州| 来安县| 白沙| 溧水县| 博兴县| 思茅市| 香格里拉县| 濮阳市| 黎城县| 绥棱县| 普洱| 兴海县| 湘西| 株洲市| 苗栗市| 淮北市| 松滋市| 盈江县| 葵青区| 武威市|