首頁 >後端開發 >Python教學 >python中time模組與datetime模組的詳解

python中time模組與datetime模組的詳解

黄舟
黄舟原創
2017-10-07 11:40:411613瀏覽


時間上下限

import datetime
print 'min    :    ', datetime.datetime.min
print 'max    :    ', datetime.datetime.max
min    :     0001-01-01 00:00:00
max    :     9999-12-31 23:59:59.999999

datetime.datetime.now() 模組

import datetime
print '.now()       :    ', datetime.datetime.now()
print type(datetime.datetime.now())
print '.now().date():    ', datetime.datetime.now().date()
print type(datetime.datetime.now().date())
print '.strftime    :    ', datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
print type(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
.now()       :     2017-09-06 19:46:23.099767<type &#39;datetime.datetime&#39;>
.now().date():     2017-09-06<type &#39;datetime.date&#39;>
.strftime    :     2017-09-06 19:46:23<type &#39;str&#39;>

取得單一時間資訊

import datetime
print &#39;year   :    &#39;, datetime.datetime.now().year
print &#39;month  :    &#39;, datetime.datetime.now().month
print &#39;day    :    &#39;, datetime.datetime.now().day
print &#39;hour   :    &#39;, datetime.datetime.now().hour
print &#39;minute :    &#39;, datetime.datetime.now().minute
print &#39;second :    &#39;, datetime.datetime.now().second
year   :     2017
month  :     9
day    :     6
hour   :     19
minute :     47
second :     13

datetime 其他模組

import datetime
print &#39;.time()      :    &#39;, datetime.time()
print &#39;.date.today():    &#39;, datetime.date.today()
.time()      :     00:00:00
.date.today():     2017-09-06

計算非今日的時間資訊

import datetime
print &#39;tomorrow:    &#39;, datetime.date.today() + datetime.timedelta(days=1)
print &#39;tomorrow:    &#39;, datetime.datetime.now() + datetime.timedelta(days=1)
print &#39;tomorrow:    &#39;, (datetime.datetime.now() + datetime.timedelta(days=1)).strftime("%Y-%m-%d %H:%M:%S")
tomorrow:     2017-09-07
tomorrow:     2017-09-07 19:49:16.292580
tomorrow:     2017-09-07 19:49:16

time 模組顯示時間

import timeprint &#39;time.time()     :    &#39;, time.time() # return当前时间戳
print &#39;time.localtime():    &#39;, time.localtime()
time.time()     :     1504698623.85
time.localtime():     time.struct_time(tm_year=2017, tm_mon=9, tm_mday=6, tm_hour=19, tm_min=50, tm_sec=23, tm_wday=2, tm_yday=249, tm_isdst=0)

延遲執行緒呼叫

import time
time.sleep(2) # 延迟2秒

以上是python中time模組與datetime模組的詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn