You are currently viewing time & datetime

time & datetime

以下說明 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))

時間替換參數對照表

DirectiveMeaningNotes
%aLocale’s abbreviated weekday name.星期幾英文縮寫
%ALocale’s full weekday name.星期幾英文
%bLocale’s abbreviated month name.月份英文縮寫
%BLocale’s full month name.月份英文
%cLocale’s appropriate date and time representation.%a %b %d %X %Y
%dDay of the month as a decimal number [01,31].日期
%HHour (24-hour clock) as a decimal number [00,23].小時 24 小時制
%IHour (12-hour clock) as a decimal number [01,12].小時 12 小時制
%jDay of the year as a decimal number [001,366].一年中的第幾天
%mMonth as a decimal number [01,12].月份
%MMinute as a decimal number [00,59].分鐘
%pLocale’s equivalent of either AM or PM.AM or PM
%SSecond as a decimal number [00,61].秒數
%UWeek 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.
%wWeekday as a decimal number [0(Sunday),6].以數字表達星期幾 0 是星期日
%WWeek 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.
%xLocale’s appropriate date representation.%m/%d/%y
%XLocale’s appropriate time representation.%H:%M:%S
%yYear without century as a decimal number [00,99].年分後2 2017 顯示 17
%YYear with century as a decimal number.完整年
%zTime 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 所差距時間
%ZTime zone name (no characters if no time zone exists).
%%A literal ‘%’ character.

Beck Yeh

熱愛學習於 Linux 與 程式設計 在網站中分享各式各樣學習到的新知識

發佈留言

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料