python年份格式化 Python日期格式化

2021-10-19 21:27:23 字數 1535 閱讀 1133

摘要: python日期格式化常用的一些格式化佔位符 %a abbreviated weekday name %a full weekday name %b abbreviated month name %b full month name %c date and time representatio

python日期格式化常用的一些格式化佔位符

%a abbreviated weekday name

%a full weekday name

%b abbreviated month name

%b full month name

%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

%% percent sign

%f millisecond

注:python不支援%z, %z等時區的格式佔位符

字串->日期格式

from datetime import datetime

tstring = '1990-7-18-23-11-20'

format = '%y-%m-%d-%h-%m-%s'

t = datetime.strptime(tstring, format)

日期格式->字串

from datetime import datetime

t = datetime.now()

format = '%y-%m-%d-%h-%m-%s'

tstring = t.strftime(format)

LocalDateTime格式化日期年份錯誤

今天 2020 12 28 在使用之前寫的乙個日期格式化的時候,發現了乙個問題,年份不是2020,而是2021。檢視了 和呼叫之後,均沒找到問題。public string getformatdatestring 最後只能看官方的使用說明。最後在datetimeformatter的說明裡找到了原因。...

python 格式化和format格式化

格式 name flags width precision typecode name 可選,用於選擇指定的key flags 可選,可提供的值有 右對齊,整數前加正號,負數前加負號 左對齊,正數錢無符號,負數前加負號 空格 右對齊 正數前加空格,負數前加負號 0 右對齊,正數前無符號,負數前加負號...

Python 格式化字元

字串格式化操作符 只適用於字串型別,語法如下 format string string to convert format string為格式標記字串,形式為 cdoe string to convert 為要格式化的字串,如果是兩個以上,則需要用小括號括起來。格式化符號 說明 r 轉換成字元 as...