python對時間日期做格式化

2021-07-02 12:19:52 字數 2282 閱讀 5074

>>> import datetime

>>> dir(datetime)

['maxyear', 'minyear', '__doc__', '__file__', '__name__', '__package__', 'date', 'datetime', 'datetime_capi', 'time', 'timedelta', 'tzinfo']

>>> datetime.datetime.now()

datetime.datetime(2015, 5, 11, 17, 29, 14, 476759)

python格式化日期時間的函式為datetime.datetime.strftime();

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

abbreviated weekday name %a

full weekday name

%b abbreviated month name

%b full month name

%c

%d day of monthasdecimal number (01 - 31)

%h hourin24-hour format (00 - 23)

%i hourin12-hour format (01 - 12)

%j day of yearasdecimal number (001 - 366)

%m monthasdecimal number (01 - 12)

%m minuteasdecimal number (00 - 59)

%p current locale's a.m./p.m. indicator for 12-hour clock

%s secondasdecimal number (00 - 59)

%u week of yearasdecimal number,withsundayasfirst day of week (00 - 51)

%w weekdayasdecimal number (0 - 6;sundayis0)

%w week of yearasdecimal number,withmondayasfirst day of week (00 - 51)

%x date representationforcurrent locale

%x time representationforcurrent locale

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

%y yearwithcentury,asdecimal number

%z,%z

time-zone nameorabbreviation; no charactersiftime zoneisunknown

%% percent sign

舉乙個例子:

ebay中時間格式為『sep-21-09 16:34』

則通過下面**將這個字串轉換成datetime

>>> c = datetime.datetime.strptime('sep-21-09 16:34','%b-%d-%y %h:%m');

>>> c

datetime.datetime(2009, 9, 21, 16, 34)

又如:datetime轉換成字串

>>> datetime.datetime.now().strftime('%b-%d-%y %h:%m:%s')

'may-11-15 17:29:29'

python對時間日期做格式化

python格式化日期時間的函式為datetime.datetime.strftime 由字串轉為日期型的函式為 datetime.datetime.strptime 兩個函式都涉及日期時間的格式化字串,列舉如下 舉乙個例子 ebay中時間格式為 sep 21 09 16 34 則通過下面 將這個字...

對時間格式化

練習 實現 public class datetimetest else else public class datetime 小月 private static int smallmonth new int 分割時間日期 param str 要處理的日曆 return 分割後的時間日期 publi...

Jquery對時間進行判斷,格式化

1.獲取當前時間,並進行格式化 var mydate new date 獲取當前年 var year mydate.getfullyear 獲取當前月 var month mydate.getmonth 1 獲取當前日 var date mydate.getdate var h mydate.get...