python time localtimeq获取准确时间_python的内置模块time和datetime的方法详解以及使用(python内的time和datetime时间格式)...
time內(nèi)置模塊的方法
1、time() 時(shí)間戳
time() -> floating point number? 浮點(diǎn)數(shù)
Return the current time in seconds since the Epoch.
Fractions of a second may be present if the system clock provides them.
importtimeprint(time.time())
C:\python35\python3.exe D:/pyproject/day21模塊/time模塊.py1528517838.7509072
這個(gè)時(shí)間戳是一個(gè)秒數(shù),是從1970年凌晨開始算,到現(xiàn)在一共經(jīng)歷了多少秒
現(xiàn)在是2018年,減去1970年,是48年,
48*365*24*60*60=1513728000
哈哈算出來跟上面的差不多,這就是時(shí)間戳,每一秒都不一樣
時(shí)間戳可以可以用來計(jì)算2個(gè)時(shí)間的減法,就是比如我下單的時(shí)候是一個(gè)時(shí)間戳,我支付成功再來一個(gè)時(shí)間戳,可以計(jì)算一下我下單到支付花了多少秒
2、localtime(seconds=None) 結(jié)構(gòu)化時(shí)間-當(dāng)?shù)貢r(shí)間
得到的是一個(gè)結(jié)構(gòu)化時(shí)間
Convert seconds since the Epoch to a time tuple expressing local time.
When 'seconds' is not passed in, convert the current time instead
importtimeprint(time.localtime())
C:\python35\python3.exe D:/pyproject/day21模塊/time模塊.py
time.struct_time(tm_year=2018, tm_mon=6, tm_mday=9, tm_hour=12, tm_min=36, tm_sec=7, tm_wday=5, tm_yday=160, tm_isdst=0)
那么我們就可以取出來具體的其中的具體的年份或者是時(shí)分秒,一周的第幾天,一年的第幾天
importtime
a=time.localtime()print(a.tm_year,a.tm_mon,a.tm_mday,a.tm_hour,":",a.tm_min,":",a.tm_sec)
C:\python35\python3.exe D:/pyproject/day21模塊/time模塊.py2018 6 9 12 : 46 : 1
3、gmtime? 也是結(jié)構(gòu)化時(shí)間 世界標(biāo)準(zhǔn)化時(shí)間-UTC
時(shí)間標(biāo)準(zhǔn)時(shí)間,跟我們的時(shí)間差8個(gè)小時(shí)
4、mktime(p_tuple)
mktime(tuple) -> floating point number
將結(jié)構(gòu)化時(shí)間轉(zhuǎn)換成時(shí)間戳
importtimeprint(time.mktime(time.localtime()))
C:\python35\python3.exe D:/pyproject/day21模塊/time模塊.py1528522939.0
5、strftime(format, p_tuple=None)
將結(jié)構(gòu)化時(shí)間轉(zhuǎn)化成字符串時(shí)間
%Y Year with century as a decimal number.%m Month as a decimal number [01,12].%d Day of the month as a decimal number [01,31].%H Hour (24-hour clock) as a decimal number [00,23].%M Minute as a decimal number [00,59].%S Second as a decimal number [00,61].%z Time zone offset fromUTC.%a Locale's abbreviated weekday name.
%A Locale's full weekday name.
%b Locale's abbreviated month name.
%B Locale's full month name.
%c Locale's appropriate date and time representation.
%I Hour (12-hour clock) as a decimal number [01,12].%p Locale's equivalent of either AM or PM.
%X就代表時(shí)分秒
importtimeprint(time.strftime("%Y-%m-%d %X",time.localtime()))
C:\python35\python3.exe D:/pyproject/day21模塊/time模塊.py2018-06-09 15:11:04
6、strptime(string, format)
將字符串時(shí)間轉(zhuǎn)化為結(jié)構(gòu)化時(shí)間
%Y Year with century as a decimal number.%m Month as a decimal number [01,12].%d Day of the month as a decimal number [01,31].%H Hour (24-hour clock) as a decimal number [00,23].%M Minute as a decimal number [00,59].%S Second as a decimal number [00,61].%z Time zone offset fromUTC.%a Locale's abbreviated weekday name.
%A Locale's full weekday name.
%b Locale's abbreviated month name.
%B Locale's full month name.
%c Locale's appropriate date and time representation.
%I Hour (12-hour clock) as a decimal number [01,12].%p Locale's equivalent of either AM or PM.
這里的字符串時(shí)間得和后面的結(jié)構(gòu)化時(shí)間一一對應(yīng)才行
importtimeprint(time.strptime("2018:06:09-15:21:36","%Y:%m:%d-%X"))
C:\python35\python3.exe D:/pyproject/day21模塊/time模塊.py
time.struct_time(tm_year=2018, tm_mon=6, tm_mday=9, tm_hour=15, tm_min=21, tm_sec=36, tm_wday=5, tm_yday=160, tm_isdst=-1)
7、asctime(p_tuple=None)可以加結(jié)構(gòu)化參數(shù),不加參數(shù)默認(rèn)是當(dāng)前時(shí)間
如果你沒有自定義需求時(shí)間格式的話,可以之間用這個(gè)方法
Convert a time tuple to a string, e.g. 'Sat Jun 06 16:26:11 1998'.
When the time tuple is not present, current time as returned by localtime()
is used
importtimeprint(time.asctime())
C:\python35\python3.exe D:/pyproject/day21模塊/time模塊.py
Sat Jun9 15:26:45 2018
8、ctime(seconds=None)可以加時(shí)間戳參數(shù),不加參數(shù)默認(rèn)是當(dāng)前時(shí)間
跟7這個(gè)asctime出來的格式是一樣的
print(time.ctime())#不加參數(shù),默認(rèn)是當(dāng)前時(shí)間
Sat Jun9 15:34:30 2018
print(time.ctime(1228629586.2798274))#加上時(shí)間戳(字符串時(shí)間)參數(shù)
Sun Dec7 13:59:46 2008
9、datetime? 這個(gè)相對來說跟好用,第一種用法比較精細(xì),第二種格式也比較好看
importdatetimeprint(datetime.datetime.now())print(datetime.datetime.now().strftime("%Y-%m-%d %X"))
C:\python35\python3.exe D:/pyproject/day21模塊/time模塊.py2018-06-09 15:44:29.870926
2018-06-09 15:44:29
總結(jié)
以上是生活随笔為你收集整理的python time localtimeq获取准确时间_python的内置模块time和datetime的方法详解以及使用(python内的time和datetime时间格式)...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux shell 博客,【博客侠】
- 下一篇: oracle创建主键开并行,Oracle