datetime中時間的formatter整理


datetime是個很常用的模塊,這個連python初學者都應該知道,datetime中有兩個函數:strftime和strptime,里面都有個參數format,可以將輸出的時間格式化。例如
print datetime.datetime.now().strftime("%Y.%m.%d")
就會顯示類似2016.9.6這樣的結果。但是百分號后面這些東西,這個我是真的記不住,每次都要百度查(桑心臉。所以特意從手冊中把全部格式都整理出來,留作紀念。
不對,留作以后忘了查詢。
 

Directive

Meaning

%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.

一個適當的“日期+時間”表示法,我這里的結果是“月/日/年 時:分:秒”。

%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/PM表示。僅與%I一塊使用。

%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.

現在是一年中的第幾個星期。從第一個星期日開始算第1個星期,之前算第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.

現在是一年中的第幾個星期。從第一個星期一開始算第1個星期,之前算第0個。

%x

Locale’s appropriate date representation.

一個適當的“日期”表示法,我這里的結果是“月/日/年”。

%X

Locale’s appropriate time representation.

一個適當的“時間”表示法,我這里的結果是“時:分:秒”。

%y

Year without century as a decimal number [00,99].

現在年(后兩位)。

%Y

Year with century as a decimal number.

現在年(完整四位)。

%Z

Time zone name (no characters if no time zone exists).

時區名稱(如果沒有時區則空白)。

%%

A literal '%' character.

百分號的轉義符。

 

如果看不明白具體長什么樣,可以用本文開頭那條語句來測試。

就醬。

PS:博客園的表格好坑啊!and圖沒有什么用,但是如果不加的話,就給什么亂七八糟的東西打廣告了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM