Python 的時間格式化

2022-02-10 23:20:19 字數 1516 閱讀 8663

對於像'wed, 11 apr 2012 09:37:05 +0800'的時間格式化可如下解:

>>> date='wed, 11 apr 2012 09:37:05 +0800'

>>> dd=datetime.datetime.strptime(date,'%a, %d %b %y %h:%m:%s %z')

>>> dd.strftime('%y-%m-%d %h:%m:%s')

python格式化日期時間的函式為datetime.datetime.strftime();由字串轉為日期型的函式為:datetime.datetime.strptime(),兩個函式都涉及日期時間的格式化字串,列舉如下:

%a abbreviated weekday name   

%a full weekday name

%b abbreviated month name

%b full month name

locale

%d day of month as decimal number (01 - 31)

%h hour in 24-hour format (00 - 23)

%i hour in 12-hour format (01 - 12)

%j day of year as decimal number (001 - 366)

%m month as decimal number (01 - 12)

%m minute as decimal number (00 - 59)

%p current locale'

s a.m./p.m. indicator for 12-hour clock

%s second as decimal number (00 - 59)

%u week of year as decimal number, with sunday as first day of week (00 - 51)

%w weekday as decimal number (0 - 6; sunday is

0)

%w week of year as decimal number, with monday as first day of week (00 - 51)

%x date representation for

current locale

%x time representation for

current locale

%y year without century, as decimal number (00 - 99)

%y year with century, as decimal number

%z, %z time-zone name or abbreviation; no characters if time zone is

unknown

%% percent sign

python 時間格式化

import time print time.time 輸出的結果是 1279578704.6725271 但是這樣是一連串的數字不是我們想要的結果,我們可以利用time模組的格式化時間的方法來處理 time.localtime time.time 用time.localtime 方法,作用是格式化...

python時間格式化

方法 time.time 時間戳 time.clock time.sleep 睡幾秒 time.gmtime 轉換成元組 格林威治時區 time.localtime 轉成元組 本機時區 time.mktime 元組轉換成時間戳 time.strftime 元組轉換成字 time.strptime 字...

python時間格式化

import time time.time 獲取當前時間戳 import time time.time 1544961651.926242time.localtime time.time 轉換成乙個元組,包含9個元素 time.localtime time.time time.struct time...