Python3.5內(nèi)置模塊之time與datetime模塊用法實(shí)例分析
本文實(shí)例講述了Python3.5內(nèi)置模塊之time與datetime模塊用法。分享給大家供大家參考,具體如下:
1、模塊的分類
a、標(biāo)準(zhǔn)庫(Python自帶):sys、os模塊
b、開源模塊(第三方模塊)
c、自定義模塊
2、內(nèi)建模塊——time
(1)在Python中通常用以下幾種方式來表示時(shí)間:
a、時(shí)間戳:從1970年1月1日開始到當(dāng)下的時(shí)間的秒數(shù),導(dǎo)入time模塊(import time),調(diào)用time.time()方法即可。
b、格式化的時(shí)間字符串。
c、元組(struct_time),共九個(gè)元素,調(diào)用time.localtime()。
由于Python的time模塊實(shí)現(xiàn)主要調(diào)用C庫,各個(gè)平臺(tái)可能有所不同。
(2)代碼
#!/usr/bin/env python # -*- coding:utf-8 -*- # Author:ZhengzhengLiu import time print(time.time()) #獲取時(shí)間戳 time.sleep(2) #睡2秒 print(time.time()) #獲取時(shí)間戳 print(time.gmtime()) #獲取當(dāng)前世界標(biāo)準(zhǔn)時(shí)間UTC print(time.localtime()) #獲取當(dāng)前本地時(shí)間 print(time.localtime(1234567890)) #將時(shí)間戳轉(zhuǎn)換成元組形式 x = time.localtime() print(x.tm_year) #獲取本地時(shí)間的年份 print("this is 2017 day:%d" %x.tm_yday) print(time.mktime(x)) #將元組形式轉(zhuǎn)換成時(shí)間戳 #將struct_time轉(zhuǎn)化成格式化字符串的形式 #print(time.strftime("%Y-%m-%d %H:%M:%S"),x) print(time.strftime("%Y-%m-%d %H:%M:%S"),time.localtime()) # %Y:x.tm_year %m:x.tm_mon 順序可以打亂 #將格式化字符串的形式轉(zhuǎn)化成struct_time print(time.strptime("2017-09-28 19:00:47","%Y-%m-%d %H:%M:%S")) #x.tm_year:2017,x.tm_mon:09 位置順序必須一一對(duì)應(yīng) print(time.asctime()) #結(jié)構(gòu)化的元組形式轉(zhuǎn)換成形如:周月日時(shí)分秒年的形式 print(time.ctime()) #將時(shí)間戳轉(zhuǎn)化成形如:周月日時(shí)分秒年的形式
運(yùn)行結(jié)果:
1506598454.9637923
1506598456.9639068
time.struct_time(tm_year=2017, tm_mon=9, tm_mday=28, tm_hour=11, tm_min=34, tm_sec=16, tm_wday=3, tm_yday=271, tm_isdst=0)
time.struct_time(tm_year=2017, tm_mon=9, tm_mday=28, tm_hour=19, tm_min=34, tm_sec=16, tm_wday=3, tm_yday=271, tm_isdst=0)
time.struct_time(tm_year=2009, tm_mon=2, tm_mday=14, tm_hour=7, tm_min=31, tm_sec=30, tm_wday=5, tm_yday=45, tm_isdst=0)
2017
this is 2017 day:271
1506598456.0
2017-09-28 19:34:16 time.struct_time(tm_year=2017, tm_mon=9, tm_mday=28, tm_hour=19, tm_min=34, tm_sec=16, tm_wday=3, tm_yday=271, tm_isdst=0)
time.struct_time(tm_year=2017, tm_mon=9, tm_mday=28, tm_hour=19, tm_min=0, tm_sec=47, tm_wday=3, tm_yday=271, tm_isdst=-1)
Thu Sep 28 19:34:17 2017
Thu Sep 28 19:34:17 2017
(3)幾種表示時(shí)間的相互轉(zhuǎn)化
#!/usr/bin/env python # -*- coding:utf-8 -*- # Author:ZhengzhengLiu import time #導(dǎo)入時(shí)間模塊 #1.返回當(dāng)前時(shí)間戳 t = time.time() print("Current time:",t) #2.將時(shí)間戳轉(zhuǎn)換成當(dāng)?shù)貢r(shí)間的時(shí)間元祖 tup1 = time.localtime(t) print(tup1) #年:tm_year,月:tm_mon,日:tm_mday print("年份:",tup1.tm_year) print("月份:",tup1.tm_mon) print("日期:",tup1.tm_mday) #3.將時(shí)間元組轉(zhuǎn)換為時(shí)間戳(毫秒會(huì)忽略) print("Timestamp is:",time.mktime(tup1)) #4.0 獲取CPU運(yùn)行時(shí)間來獲取比較精準(zhǔn)的秒數(shù),一般用于時(shí)間間隔的獲取或比較 #4.1 休眠/停止一段時(shí)間,指定需要停止的秒數(shù) print("開始時(shí)間:",time.clock()) time.sleep(3) print("結(jié)束時(shí)間1:",time.clock()) time.sleep(3) print("結(jié)束時(shí)間2:",time.clock()) #5.時(shí)間元組轉(zhuǎn)化成格式化時(shí)間字符串 tm_str = time.strftime("%Y-%m-%d %H:%M:%S",tup1) print("格式化時(shí)間字符串:",tm_str) #等價(jià)于 print("格式化時(shí)間字符串:",time.strftime("%Y-%m-%d %H:%M:%S",tup1)) #6.格式化時(shí)間字符串轉(zhuǎn)化成時(shí)間元組 tup2 = time.strptime(tm_str,"%Y-%m-%d %H:%M:%S") print("時(shí)間元組:",tup2) #等價(jià)于 print("時(shí)間元組:",time.strptime(tm_str,"%Y-%m-%d %H:%M:%S"))
運(yùn)行結(jié)果:
Current time: 1509503024.233398
time.struct_time(tm_year=2017, tm_mon=11, tm_mday=1, tm_hour=10, tm_min=23, tm_sec=44, tm_wday=2, tm_yday=305, tm_isdst=0)
年份: 2017
月份: 11
日期: 1
Timestamp is: 1509503024.0
開始時(shí)間: 1.399506347461039e-06
結(jié)束時(shí)間1: 2.9997266297601293
結(jié)束時(shí)間2: 5.999885706981624
格式化時(shí)間字符串: 2017-11-01 10:23:44
格式化時(shí)間字符串: 2017-11-01 10:23:44
時(shí)間元組: time.struct_time(tm_year=2017, tm_mon=11, tm_mday=1, tm_hour=10, tm_min=23, tm_sec=44, tm_wday=2, tm_yday=305, tm_isdst=-1)
時(shí)間元組: time.struct_time(tm_year=2017, tm_mon=11, tm_mday=1, tm_hour=10, tm_min=23, tm_sec=44, tm_wday=2, tm_yday=305, tm_isdst=-1)
3、內(nèi)建模塊——datatime
(1)datatime.data——日期(年月日)
(2)datatime.time——時(shí)間(時(shí)分秒)
(3)datatime.datatime——日期+時(shí)間(年月日時(shí)分秒)
代碼舉例:
import datetime print(datetime.datetime.now()) #獲取當(dāng)前時(shí)間 print(datetime.datetime.now()+datetime.timedelta(3)) #當(dāng)前時(shí)間的后三天 print(datetime.datetime.now()+datetime.timedelta(-3)) #當(dāng)前時(shí)間的前三天 print(datetime.datetime.now()+datetime.timedelta(hours=3)) #當(dāng)前時(shí)間的后三個(gè)小時(shí) print(datetime.datetime.now()+datetime.timedelta(minutes=-3)) #當(dāng)前時(shí)間的前三分鐘 c_time = datetime.datetime.now() print(c_time.replace(minute=2,hour=3)) #對(duì)當(dāng)前時(shí)間的替換
運(yùn)行結(jié)果:
2017-09-28 19:54:40.167870
2017-10-01 19:54:40.167870
2017-09-25 19:54:40.167870
2017-09-28 22:54:40.167870
2017-09-28 19:51:40.167870
2017-09-28 03:02:40.167870
PS:這里再為大家推薦幾款關(guān)于日期與天數(shù)計(jì)算的在線工具供大家使用:
在線日期/天數(shù)計(jì)算器:
http://tools.jb51.net/jisuanqi/date_jisuanqi
在線萬年歷日歷:
http://tools.jb51.net/bianmin/wannianli
在線陰歷/陽歷轉(zhuǎn)換工具:
http://tools.jb51.net/bianmin/yinli2yangli
Unix時(shí)間戳(timestamp)轉(zhuǎn)換工具:
http://tools.jb51.net/code/unixtime
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python日期與時(shí)間操作技巧總結(jié)》、《Python數(shù)學(xué)運(yùn)算技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python Socket編程技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門與進(jìn)階經(jīng)典教程》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
詳解如何使用Python處理INI、YAML和JSON配置文件
在軟件開發(fā)中,配置文件是存儲(chǔ)程序配置信息的常見方式,INI、YAML和JSON是常用的配置文件格式,各自有著特定的結(jié)構(gòu)和用途,Python擁有豐富的庫和模塊,本文將重點(diǎn)探討如何使用Python處理這三種格式的配置文件,需要的朋友可以參考下2023-12-12Django模板繼承與模板的導(dǎo)入實(shí)例詳解
模板繼承主要是為了提高代碼重用,減輕開發(fā)人員的工作量,下面這篇文章主要給大家介紹了關(guān)于Django模板繼承與模板導(dǎo)入的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-03-03python實(shí)現(xiàn)圖像識(shí)別功能
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)圖像識(shí)別功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01python中from module import * 的一個(gè)坑
from module import *把module中的成員全部導(dǎo)到了當(dāng)前的global namespace,訪問起來就比較方便了。當(dāng)然,python style一般不建議這么做,因?yàn)榭赡芤餹ame conflict。2014-07-07關(guān)于Python Tkinter Button控件command傳參問題的解決方式
這篇文章主要介紹了關(guān)于Python Tkinter Button控件command傳參問題的解決方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03Python?解釋器的站點(diǎn)配置和模塊搜索路徑詳解
Python?解釋器的站點(diǎn)配置是指一組配置和路徑設(shè)置,用于支持特定于站點(diǎn)的定制和擴(kuò)展,這些配置和路徑信息由?Python?的內(nèi)置?site?模塊提供,這篇文章主要介紹了Python?解釋器的站點(diǎn)配置和模塊搜索路徑詳解,需要的朋友可以參考下2022-01-01基于Python繪制一個(gè)摸魚倒計(jì)時(shí)界面
前端時(shí)間推出了一個(gè)摸魚APP,這篇文章將為大家介紹基于Python繪制一個(gè)摸魚倒計(jì)時(shí)界面,文中的示例代碼講解詳細(xì),感興趣的可以學(xué)習(xí)一下2021-12-12python fabric實(shí)現(xiàn)遠(yuǎn)程操作和部署示例
這篇文章主要介紹了python使用fabric實(shí)現(xiàn)遠(yuǎn)程操作和部署示例,需要的朋友可以參考下2014-03-03