国产探花免费观看_亚洲丰满少妇自慰呻吟_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程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 扎兰屯市| 磐安县| 珲春市| 保亭| 苍梧县| 阳新县| 万源市| 东安县| 富顺县| 昆明市| 凌云县| 娄底市| 娱乐| 舞阳县| 晋中市| 菏泽市| 巫溪县| 永泰县| 赤水市| 龙胜| 溧阳市| 沈阳市| 保靖县| 洛南县| 达日县| 凌源市| 大庆市| 高碑店市| 宁海县| 富锦市| 忻城县| 沁水县| 赣州市| 太谷县| 灌南县| 阜阳市| 塔河县| 明星| 景宁| 民丰县| 芜湖县|