본문 바로가기
python/Django

Django templates 날짜(datetime) 포맷 변경 및 UTC 설정

by 맑은안개 2021. 2. 3.

datetime 형식에 대해 templates에 적용되는 default time format은 아래와 같다.

DATETIME_FORMAT

Default: 'N j, Y, P' (e.g. Feb. 4, 2003, 4 p.m.)

 

Built-in Filter date 사용하여 변경

  Django에서 제공하는 Filter기능을 사용하여 templates에 사용되는 변수를 변경할 수 있다.

{{ data.pub_date|date:'Y-m-d H:i' }}

 

Date display & UTC localizing 

  settings.py를 아래와 같이 수정하면 로컬라이징된 날짜를 출력할 수 있다.

LANGUAGE_CODE = 'ko-KR'
TIME_ZONE = 'Asia/Seoul' # default UTC, Asia/Seoul은 +9 hours
USE_TZ = True

다음과 같이 출력된다. 2021년 2월 2일 5:01 오전 

 

 

Date filter 사용 포맷문자열( PHP date format과 동일함 ) - 출처 레퍼런스 사이트

Day    
d Day of the month, 2 digits with leading zeros. '01' to '31'
j Day of the month without leading zeros. '1' to '31'
D Day of the week, textual, 3 letters. 'Fri'
l Day of the week, textual, long. 'Friday'
S English ordinal suffix for day of the month, 2 characters. 'st', 'nd', 'rd' or 'th'
w Day of the week, digits without leading zeros. '0' (Sunday) to '6' (Saturday)
z Day of the year. 1 to 366
Week    
W ISO-8601 week number of year, with weeks starting on Monday. 1, 53
Month    
m Month, 2 digits with leading zeros. '01' to '12'
n Month without leading zeros. '1' to '12'
M Month, textual, 3 letters. 'Jan'
b Month, textual, 3 letters, lowercase. 'jan'
E Month, locale specific alternative representation usually used for long date representation. 'listopada' (for Polish locale, as opposed to 'Listopad')
F Month, textual, long. 'January'
N Month abbreviation in Associated Press style. Proprietary extension. 'Jan.', 'Feb.', 'March', 'May'
t Number of days in the given month. 28 to 31
Year    
y Year, 2 digits. '99'
Y Year, 4 digits. '1999'
L Boolean for whether it’s a leap year. True or False
o ISO-8601 week-numbering year, corresponding to the ISO-8601 week number (W) which uses leap weeks. See Y for the more common year format. '1999'
Time    
g Hour, 12-hour format without leading zeros. '1' to '12'
G Hour, 24-hour format without leading zeros. '0' to '23'
h Hour, 12-hour format. '01' to '12'
H Hour, 24-hour format. '00' to '23'
i Minutes. '00' to '59'
s Seconds, 2 digits with leading zeros. '00' to '59'
u Microseconds. 000000 to 999999
a 'a.m.' or 'p.m.' (Note that this is slightly different than PHP’s output, because this includes periods to match Associated Press style.) 'a.m.'
A 'AM' or 'PM'. 'AM'
f Time, in 12-hour hours and minutes, with minutes left off if they’re zero. Proprietary extension. '1', '1:30'
P Time, in 12-hour hours, minutes and ‘a.m.’/’p.m.’, with minutes left off if they’re zero and the special-case strings ‘midnight’ and ‘noon’ if appropriate. Proprietary extension. '1 a.m.', '1:30 p.m.', 'midnight', 'noon', '12:30 p.m.'
Timezone    
e Timezone name. Could be in any format, or might return an empty string, depending on the datetime. '', 'GMT', '-500', 'US/Eastern', etc.
I Daylight Savings Time, whether it’s in effect or not. '1' or '0'
O Difference to Greenwich time in hours. '+0200'
T Time zone of this machine. 'EST', 'MDT'
Z Time zone offset in seconds. The offset for timezones west of UTC is always negative, and for those east of UTC is always positive. -43200 to 43200
Date/Time    
c ISO 8601 format. (Note: unlike other formatters, such as “Z”, “O” or “r”, the “c” formatter will not add timezone offset if value is a naive datetime (see datetime.tzinfo). 2008-01-02T10:30:00.000123+02:00, or 2008-01-02T10:30:00.000123 if the datetime is naive
r RFC 5322 formatted date. 'Thu, 21 Dec 2000 16:01:07 +0200'
U Seconds since the Unix Epoch (January 1 1970 00:00:00 UTC).
반응형