Python中的time模塊與datetime模塊用法總結(jié)
time模塊
time模塊是包含各方面對(duì)時(shí)間操作的函數(shù). 盡管這些常常有效但不是所有方法在任意平臺(tái)中有效. time用struct_time表示時(shí)間
import time # time.struct_time(tm_year=2015, tm_mon=4, tm_mday=24, tm_hour=14, tm_min=17, tm_sec=26, tm_wday=4, tm_yday=114, tm_isdst=0) # 2015 print time.localtime() print time.localtime().tm_year
函數(shù)
- time.time(): 返回一個(gè)時(shí)間戳
- time.asctime([t]): 轉(zhuǎn)換gmtime()和localtime()返回的元組或struct_time為string.
- time.clock(): 在第一次調(diào)用的時(shí)候, 返回程序運(yùn)行的時(shí)間. 第二次之后返回與之前的間隔.
- time.ctime([secs]): 將時(shí)間戳轉(zhuǎn)換為時(shí)間字符串, 如沒(méi)有提供則返回當(dāng)前的時(shí)間字符串,并與asctime(localtime())一樣.
- time.gmtime([secs]): 將時(shí)間戳轉(zhuǎn)化為, UTC 時(shí)區(qū)的struct_time.
- time.localtime([secs]): 類似gmtime()但會(huì)把他轉(zhuǎn)換成本地時(shí)區(qū).
- time.mktime(t): struct_time 轉(zhuǎn)化為時(shí)間戳.
- time.sleep(secs): 線程推遲指定時(shí)間, 以秒為單位.
- time.strftime(format[,t]): 根據(jù)參數(shù)轉(zhuǎn)換一個(gè)sturc_time或元組為字符串.
- time.strptime(string[, format]): 與strftime相反,返回一個(gè)struct_time.
import time # Fri Apr 24 06:39:34 2015 print time.asctime(time.gmtime()) # 0.0 # None # 1.01136392961 因計(jì)算機(jī)而異 print time.clock() print time.sleep(1) print time.clock() # Fri Apr 24 14:42:07 2015 print time.ctime() # 2015-04-24 print time.strftime('%Y-%m-%d', time.localtime()) # 1429857836.0 print time.mktime(time.localtime())
time模塊中常用的格式化字符串
- %y 兩位數(shù)的年份 00 ~ 99.
- %Y 四位數(shù)的年份 0000 ~ 9999
- %m 月份 01 ~ 12.
- %d day 01 ~ 31.
- %H 時(shí) 00 ~ 23.
- %I 時(shí) 01 ~ 12.
- %M 分 00 ~ 59.
- %S 秒 00 ~ 61.
datetime模塊
datetime模塊提供對(duì)于日期和時(shí)間進(jìn)行簡(jiǎn)單或復(fù)雜的操作. datetime 模塊提供了一下的可用類型(Available Types).
datetime.MINYEAR 和 datetime.MAXYEAR 模塊常量表示datetime接受的范圍
- class datetime.date: 一個(gè)理想化的日期, 提供year, month, day屬性
- class datetime.time: 一個(gè)理想化的時(shí)間, 提供hour, minute, second, microsecond, tzinfo.
- class datetime.datetime: 日期和時(shí)間的組合.提供year, month, day, hour, minute, second, microsecond, tzinfo.
- class datetime.timedelta: 表達(dá)兩個(gè)date,time和datetime持續(xù)時(shí)間內(nèi)的微妙差異.
- class datetime.tzinfo: 時(shí)間對(duì)象的抽象基類.
from datetime import timedelta, datetime a = datetime.now() b = timedelta(days=7) # 7 days, 0:00:00 # 2015-04-14 16:02:39.189000 print b print a - b
下面說(shuō)具體說(shuō)一下類和類的方法
date類
一個(gè)date對(duì)象代表理想化的日期.
class datetime.date(year, month, day) # All arguments are required. Arguments may be ints or longs. # 所有參數(shù)都是必須的. 參數(shù)可能是 int 或 long. MINYEAR <= year <= MAXYEAR 1<= month <= 12 1<= day <= number of days in the given month and year.(隨著月份和年份)
如果參數(shù)脫離給的范圍會(huì)拋出, valueError.
1.類方法 >`date.today()`:返回當(dāng)前的本地日期, 這等價(jià)于 `date.fromtimestamp(time.time())`.
Return the current local date. This is equvalent to `date.fromtimestamp(time.time())`.
from datetime import date # print 2015-04-21 print date.today()
2.date.fromtimestamp(timestamp):根據(jù)提供的時(shí)間戳返回local date. 時(shí)間戳常用于對(duì)時(shí)間類型的存儲(chǔ).
import time from datetime import date # 1429587111.21 # 2015-04-21 print time.time() print date.fromtimestamp(time.time())
3.類方法date.fromordinal(ordinal):根據(jù)提供的Gregorian日歷返回date.(不做描述)
類屬性
- date.min: 返回 date(MINYEAR, 1, 1).
- date.max: 返回 date(MAXYEAR, 12, 31).
- date.year: 返回 年, MINYEAR和MAXYEAR之間
- date.month: 返回 月, 1到12月之間
- date.day: 返回 1到 n 之間.
d = date(2014, 4, 21) # 2014 4 21 print d.year, d.month, d.day
實(shí)例方法
- date.replace(year, month, day):返回一個(gè)相同值的data對(duì)象, 除了這些參數(shù)給關(guān)鍵字指定新的值.
- date.timetuple(): 返回一個(gè)time.struct_time對(duì)象.
- date.toordinal(): 返回一個(gè)Gregoian Calendar對(duì)象.
- date.weekday(): 返回day of the week. 星期一為0,星期日為6.
- date.isoweekday(): 返回day of the week. 星期一為1,星期日為7.
- date.isocalendar(): 返回一個(gè)三元組, (ISO year, ISO week number, ISO weekday).
- date.isoformat(): 返回 一個(gè)'YYYY-MM-DD'的字符串格式.
- date.ctime(): 返回一個(gè)字符串日期, d.ctime() 等同于 time.ctime(time.mktime(d.timetuple())).
- date.strftime(format): 返回一個(gè)字符串日期, 格式自定義.
d = date(2015, 4, 21) # 2015-04-21 # 2015-04-21 # 2015-04-22 print d print d.replace() print d.replace(day=22) # time.struct_time(tm_year=2015, tm_mon=4, tm_mday=21, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=1, tm_yday=111, tm_isdst=-1) print d.timetuple() # print 1 # print 2 print d.weekday() print d.isoweekday() # print 2015-04-21 print d.isoformat() # print 21/04/2015 print d.strftime('%d/%m/%y')
datetime 類
datetime 對(duì)象是一個(gè)單一的對(duì)象, 包含所有date和time對(duì)象的信息.
class datetime.datetime(year, month, day[, hour [, minute [, second [, microsecond [, tzinfo]]]]]) # The year, month and day arguments are required. MINYEAR <= year <= MAXYEAR 1 <= month <= 12 1 <= day <= n 0 <= hour < 24 0 <= minute < 60 0 <= second < 60 0 <= microsecond < 10**6
類方法
- datetime.today(): 返回當(dāng)前本地datetime.隨著 tzinfo None. 這個(gè)等同于datetime.fromtimestamp(time.time()).
- datetime.now([tz]): 返回當(dāng)前本地日期和時(shí)間, 如果可選參數(shù)tz為None或沒(méi)有詳細(xì)說(shuō)明,這個(gè)方法會(huì)像today().
- datetime.utcnow(): 返回當(dāng)前的UTC日期和時(shí)間, 如果tzinfo None ,那么與now()類似.
- datetime.fromtimestamp(timestamp[, tz]): 根據(jù)時(shí)間戳返回本地的日期和時(shí)間.tz指定時(shí)區(qū).
- datetime.utcfromtimestamp(timestamp): 根據(jù)時(shí)間戳返回 UTC datetime.
- datetime.fromordinal(ordinal): 根據(jù)Gregorian ordinal 返回datetime.
- datetime.combine(date, time): 根據(jù)date和time返回一個(gè)新的datetime.
- datetime.strptime(date_string, format): 根據(jù)date_string和format返回一個(gè)datetime.
from datetime import datetime # 2015-04-21 14:07:39.262000 print datetime.today() # 2015-04-21 14:08:20.362000 print datetime.now() # 1429596607.06 # 2015-04-21 14:10:07.061000 t = time.time() print t print datetime.fromtimestamp(t) from datetime import datetime, date, time a = date(2015, 4, 21) b = time(14, 13, 34) # 2015-04-21 14:13:34 print datetime.combine(a, b)
實(shí)例方法
- datetime.date(): 返回相同年月日的date對(duì)象.
- datetime.time(): 返回相同時(shí)分秒微秒的time對(duì)象.
- datetime.replace(kw): kw in [year, month, day, hour, minute, second, microsecond, tzinfo], 與date類似.
其他方法可查看官方文檔…
from datetime import datetime, date, time td = date(2015, 4, 21) n = time(14, 28, 30) # 2099-04-21 14:30:42.103000 print datetime.now(0.replace(year=2099)
類屬性
- datetime.min: datetime(MINYEAR, 1, 1).
- datetime.max: datetime(MAXYEAR, 12, 31, 23, 59, 59, 999999).
實(shí)例屬性(read-only)
- datetime.year: 1 至 9999
- datetime.month: 1 至 12
- datetime.day: 1 至 n
- datetime.hour: In range(24). 0 至 23
- datetime.minute: In range(60).
- datetime.second: In range(60).
- datetime.microsecond: In range(1000000).
time類
time 代表本地(一天內(nèi))時(shí)間.
class datetime.time([hour [, minute [, second [, microsecond [, tzinfo]]]]]) # All arguments are optional. # 所有參數(shù)都是可選的. 0 <= hour < 24 0 <= minute < 60 0 <= second < 60 0 <= microsesond < 10**6
time類就是對(duì)時(shí)間的一些操作,其功能類似與datetime.其實(shí)date和time就是對(duì)datetime中日期和時(shí)間的操作.
相關(guān)文章
python 讀txt文件,按‘,’分割每行數(shù)據(jù)操作
這篇文章主要介紹了python 讀txt文件,按‘,’分割每行數(shù)據(jù)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07python實(shí)現(xiàn)身份證實(shí)名認(rèn)證的方法實(shí)例
這篇文章主要給大家介紹了關(guān)于python實(shí)現(xiàn)身份證實(shí)名認(rèn)證的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用python具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11Python Pygame實(shí)現(xiàn)俄羅斯方塊
這篇文章主要為大家詳細(xì)介紹了Python Pygame實(shí)現(xiàn)俄羅斯方塊,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-02-02