python時間日期操作方法實例小結(jié)
本文實例講述了python時間日期操作方法。分享給大家供大家參考,具體如下:
#coding=utf-8 import time import datetime if __name__ == "__main__": # 今天 now = datetime.datetime.now() print now.strftime('%Y-%m-%d %H:%M:%S') print "%s-%s-%s %s:%s:%s" % (now.year, now.month, now.day, now.hour, now.minute, now.second) print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) # 前一天 now = datetime.datetime.now() dt = now + datetime.timedelta(days=-1) print dt.strftime('%Y-%m-%d %H:%M:%S') print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time() - 24 * 3600)) # 后一天 now = datetime.datetime.now() dt = now + datetime.timedelta(days=1) print dt.strftime('%Y-%m-%d %H:%M:%S') print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time() + 24 * 3600)) # 前一小時 now = datetime.datetime.now() dt = now - datetime.timedelta(hours=1) print dt.strftime("%Y-%m-%d %H:%M:%S") print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time() - 1 * 3600)) # 時間戳 秒 print int(time.time()) # 時間戳 毫秒 print int(round(time.time() * 1000)) # 時間戳 to 日期 print datetime.datetime.fromtimestamp(1507630854) print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(1507630854)) print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) # 日期 to 時間戳 print time.mktime(time.strptime("2017-10-10", "%Y-%m-%d")) print time.mktime(time.strptime("2017-10-10 10:10:10", "%Y-%m-%d %H:%M:%S"))
運行結(jié)果:
2020-02-06 11:33:51
2020-2-6 11:33:51
2020-02-06 11:33:51
2020-02-05 11:33:51
2020-02-05 11:33:51
2020-02-07 11:33:51
2020-02-07 11:33:51
2020-02-06 10:33:51
2020-02-06 10:33:51
1580960031
1580960031893
2017-10-10 18:20:54
2017-10-10 18:20:54
2020-02-06 11:33:51
1507564800.0
1507601410.0
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
Unix時間戳(timestamp)轉(zhuǎn)換工具:
http://tools.jb51.net/code/unixtime
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python日期與時間操作技巧總結(jié)》、《Python數(shù)學(xué)運算技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python Socket編程技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門與進(jìn)階經(jīng)典教程》
希望本文所述對大家Python程序設(shè)計有所幫助。
- python操作日期和時間的方法
- python中日期和時間格式化輸出的方法小結(jié)
- Python之日期與時間處理模塊(date和datetime)
- Python實用日期時間處理方法匯總
- Python日期時間模塊datetime詳解與Python 日期時間的比較,計算實例代碼
- python時間日期函數(shù)與利用pandas進(jìn)行時間序列處理詳解
- Python日期時間對象轉(zhuǎn)換為字符串的實例
- python獲取當(dāng)前日期和時間的方法
- python中關(guān)于日期時間處理的問答集錦
- Python正則表達(dá)式匹配日期與時間的方法
- 使用Python將字符串轉(zhuǎn)換為格式化的日期時間字符串
- Python3日期與時間戳轉(zhuǎn)換的幾種方法詳解
相關(guān)文章
Python + selenium自動化環(huán)境搭建的完整步驟
這篇文章主要給大家介紹了關(guān)于Python + selenium自動化環(huán)境搭建的相關(guān)資料,文中通過圖文將實現(xiàn)的步驟一步步介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧2018-05-05Python requests HTTP驗證登錄實現(xiàn)流程
這篇文章主要介紹了Python requests HTTP驗證登錄實現(xiàn)流程,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-11-11針對Pandas的總結(jié)以及數(shù)據(jù)讀取_pd.read_csv()的使用詳解
這篇文章主要針對Pandas總結(jié)以及數(shù)據(jù)讀取_pd.read_csv()的使用詳解做出了實例,講解非常全面,值得收藏,需要的朋友可以參考下2023-03-03python命令行參數(shù)解析OptionParser類用法實例
這篇文章主要介紹了python命令行參數(shù)解析OptionParser類用法實例,需要的朋友可以參考下2014-10-10Python drop方法刪除列之inplace參數(shù)實例
這篇文章主要介紹了Python drop方法刪除列之inplace參數(shù)實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06