python算兩個時間之間的天數,將天數轉成int型

來源:互聯網
上載者:User

標籤:%s   http   pre   com   定義函數   下標   print   date   日期   

import timeimport datetime #計算兩個日期相差天數,自訂函數名,和兩個日期的變數名。def Caltime(date1,date2):    #%Y-%m-%d為日期格式,其中的-可以用其他代替或者不寫,但是要統一,同理後面的時分秒也一樣;可以只計算日期,不計算時間。    #date1=time.strptime(date1,"%Y-%m-%d %H:%M:%S")     #date2=time.strptime(date2,"%Y-%m-%d %H:%M:%S")    date1=time.strptime(date1,"%Y/%m/%d")    date2=time.strptime(date2,"%Y/%m/%d")    #根據上面需要計算日期還是日期時間,來確定需要幾個數組段。下標0表示年,小標1表示月,依次類推...    #date1=datetime.datetime(date1[0],date1[1],date1[2],date1[3],date1[4],date1[5])    #date2=datetime.datetime(date2[0],date2[1],date2[2],date2[3],date2[4],date2[5])    date1=datetime.datetime(date1[0],date1[1],date1[2])    date2=datetime.datetime(date2[0],date2[1],date2[2])    #返回兩個變數相差的值,就是相差天數    print((date2-date1).days)#將天數轉成int型    return(date2-date1)if __name__==‘__main__‘:    print(‘請輸入較早日期(格式例:xxxx/xx/xx):‘)    dt1=input()    print(‘\n請輸入較晚日期(格式為:xxxx/xx/xx):‘)    dt2=input()    print(‘較早日期為:‘+dt1+‘  較晚日期為:‘+dt2)    print(‘\n兩個日期相差 ‘)    Caltime(dt1,dt2)    print (Caltime(dt1,dt2))    print (type(Caltime(dt1,dt2)))

python算兩個時間之間的天數,將天數轉成int型

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.