Python實現(xiàn)計算兩個時間之間相差天數(shù)的方法
本文實例講述了Python實現(xiàn)計算兩個時間之間相差天數(shù)的方法。分享給大家供大家參考,具體如下:
#-*- encoding:UTF-8 -*-
from datetime import date
import time
nowtime = date.today()
def convertstringtodate(stringtime):
"把字符串類型轉(zhuǎn)換為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 begintime
def comparetime(nowtime,stringtime):
"比較兩個時間,并返回兩個日期之間相差的天數(shù)"
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:這里再為大家推薦幾款關(guān)于日期與天數(shù)計算的在線工具供大家使用:
在線日期/天數(shù)計算器:
http://tools.jb51.net/jisuanqi/date_jisuanqi
在線萬年歷日歷:
http://tools.jb51.net/bianmin/wannianli
在線陰歷/陽歷轉(zhuǎn)換工具:
http://tools.jb51.net/bianmin/yinli2yangli
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python日期與時間操作技巧總結(jié)》、《Python URL操作技巧總結(jié)》、《Python圖片操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python Socket編程技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計有所幫助。
- python利用datetime模塊計算時間差
- python中關(guān)于時間和日期函數(shù)的常用計算總結(jié)(time和datatime)
- python計算程序開始到程序結(jié)束的運行時間和程序運行的CPU時間
- python使用datetime模塊計算各種時間間隔的方法
- Python計算程序運行時間的方法
- python計算時間差的方法
- Python日期時間模塊datetime詳解與Python 日期時間的比較,計算實例代碼
- Python執(zhí)行時間的計算方法小結(jié)
- Python計算一個給定時間點前一個月和后一個月第一天的方法
- Python3顯示當(dāng)前時間、計算時間差及時間加減法示例代碼
相關(guān)文章
Python 使用ConfigParser操作ini配置文件
這篇文章主要介紹了Python 使用ConfigParser操作ini配置文件的相關(guān)資料,需要的朋友可以參考下2023-05-05
Python數(shù)組拼接np.concatenate實現(xiàn)過程
這篇文章主要介紹了Python數(shù)組拼接np.concatenate實現(xiàn)過程,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-04-04
Python將圖片批量從png格式轉(zhuǎn)換至WebP格式
最近因為工作需要去研究了下png的壓縮,發(fā)現(xiàn)轉(zhuǎn)換成webp格式可以小很多,下面給大家分享利用Python將圖片批量從png格式轉(zhuǎn)換至WebP格式的方法,下面來一起看看。2016-08-08
Django REST Swagger實現(xiàn)指定api參數(shù)
這篇文章主要介紹了Django REST Swagger實現(xiàn)指定api參數(shù),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07

