以下說明 Python 操作時間的方法
import time
# 返回處理器時間,3.3開始已廢棄 , 改成了 time.process_time() 測量處理器運算時間
#不包括 sleep 時間,不穩定 mac 上測不出來
print(time.clock())
# 返回與 utc 時間的時間差,以秒計算
print(time.altzone)
# 返回時間格式 "Fri Aug 19 11:14:16 2016"
print(time.asctime())
# 返回本地時間的 struct time 對象格式
print(time.localtime())
# 返回 utc 時間的 struc 時間對象格式
print(time.gmtime(time.time()-800000))
# 返回時間格式 "Fri Aug 19 11:14:16 2016"
print(time.asctime(time.localtime()))
# 返回 Fri Aug 19 12:38:29 2016 格式, 同上
print(time.ctime())
# 日期字符串 轉成 時間戳
# 將日期字符串轉成 struct 時間對象格式
string_2_struct = time.strptime("2016/05/22","%Y/%m/%d")
print(string_2_struct)
# #
# 將 struct 時間對象轉成時間戳
struct_2_stamp = time.mktime(string_2_struct)
print(struct_2_stamp)
# 將時間戳轉為字符串格式
# 將utc時間戳轉換成 struct_time 格式
print(time.gmtime(time.time()-86640))
# 將 utc struct_time 格式轉成指定的字符串格式
print(time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime()) )
# 時間加減
import datetime
# 返回 2016-08-19 12:47:03.941925
print(datetime.datetime.now())
# 時間戳直接轉成日期格式 2016-08-19
print(datetime.date.fromtimestamp(time.time()) )
print(datetime.datetime.now() )
# 當前時間 +3 天
print(datetime.datetime.now() + datetime.timedelta(3))
# 當前時間 -3 天
print(datetime.datetime.now() + datetime.timedelta(-3))
# 當前時間 +3 小時
print(datetime.datetime.now() + datetime.timedelta(hours=3))
# 當前時間 +30 分
print(datetime.datetime.now() + datetime.timedelta(minutes=30))
#
c_time = datetime.datetime.now()
# 時間替換
print(c_time.replace(minute=3,hour=2))
時間替換參數對照表
Directive | Meaning | Notes |
---|---|---|
%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. | %a %b %d %X %Y |
%d | Day of the month as a decimal number [01,31]. | 日期 |
%H | Hour (24-hour clock) as a decimal number [00,23]. | 小時 24 小時制 |
%I | Hour (12-hour clock) as a decimal number [01,12]. | 小時 12 小時制 |
%j | Day of the year as a decimal number [001,366]. | 一年中的第幾天 |
%m | Month as a decimal number [01,12]. | 月份 |
%M | Minute as a decimal number [00,59]. | 分鐘 |
%p | Locale’s equivalent of either AM or PM. | AM or PM |
%S | Second as a decimal number [00,61]. | 秒數 |
%U | Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0. | |
%w | Weekday as a decimal number [0(Sunday),6]. | 以數字表達星期幾 0 是星期日 |
%W | Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0. | |
%x | Locale’s appropriate date representation. | %m/%d/%y |
%X | Locale’s appropriate time representation. | %H:%M:%S |
%y | Year without century as a decimal number [00,99]. | 年分後2 2017 顯示 17 |
%Y | Year with century as a decimal number. | 完整年 |
%z | Time zone offset indicating a positive or negative time difference from UTC/GMT of the form +HHMM or -HHMM, where H represents decimal hour digits and M represents decimal minute digits [-23:59, +23:59]. | 顯示與 UTC 所差距時間 |
%Z | Time zone name (no characters if no time zone exists). | |
%% | A literal ‘%’ character. |