1. 程式人生 > >time模塊 | datetime模塊 | Python

time模塊 | datetime模塊 | Python

本地 cpu lee ctime -1 time() oca 表示 port

import time

print(time.time())  # 1970年到現在的時間,以秒的形式
# time.sleep(1)  # 延遲,cpu不工作
print(time.clock())  # 計算CPU執行時間
print(time.gmtime())  # 結構化時間,英國世界標準時間
print(time.localtime())  # 本地時間
# print(help(time.strftime))
struct_time = time.localtime()
print(time.strftime(%Y-%m-%d %H:%M:%S, struct_time))  #
格式化時間 # 取出時間的一部分 print(time.strptime(2017-11-11 19:44:08,%Y-%m-%d %H:%M:%S)) # 轉化成結構化時間 a = time.strptime(2017-11-11 19:44:08,%Y-%m-%d %H:%M:%S) print(a.tm_year) # 哪一年 print(a.tm_mday) # 一個月的第幾天 print(a.tm_wday) # 一個星期的第幾天 # 時間戳,結構化時間,格式化時間; print(time.ctime()) # 定義好格式的當前時間 print(time.ctime(3600)) #
參數表示1970到現在的時間 print(time.mktime(time.localtime())) # 格式化時間轉化成時間戳 # ----------------------->> datetime import datetime print(datetime.datetime.now()) # 2017-11-11 19:57:53.182881

time模塊 | datetime模塊 | Python