date

See also

date_range, datediff, timesince, now

Formats a date or datetime according to the format specified in the argument.

The date should be a tuple {Y,M,D} and the datetime should be a tuple {{Y,M,D},{H,I,S}}. Dates and datetimes are always assumed to be in local time.

An example:

{{ mydate|date:"Y-m-d" }}

If mydate is {2009,6,1} this returns 2009-06-01 as output.

To show the year of the current date:

{{ now|date:"Y" }}

See also the timesince filter to display a human readable relative time like 10 hours ago.

Timezones

Dates in Zotonic are stored in UTC. If a date is displayed then it is converted to the timezone of the current request context. This timezone can be one of the following, in order of preference:

  • Preferred timezone set by the user

  • Timezone of the user-agent

  • Default timezone of the site

  • Default timezone of the Zotonic server

  • UTC

A specific timezone can be enforced by adding a second parameter to the date-filter. For example, to display a date in UTC:

{{ mydate|date:"Y-m-d H:i T":"UTC" }}

Instead of the timezone, the following arguments are also accepted:

  • true set the timezone to UTC

  • false leave the timezone as is

  • undefined leave the timezone as is

  • a resource id (integer), set the timezone according to the tz property of the resource

Timezone and all day date ranges

If a resource’s date range is set with the date_is_all_day flag then the dates are not converted to or from UTC but stored as-is. This needs to be taken into account when displaying those dates, otherwise a conversion from (assumed) UTC to the current timezone is performed and the wrong date might be displayed.

The timezone conversion can be prevented by adding the date_is_all_day flag to the date-filter as the timezone. Example, for displaying the start date of a resource:

{{ id.date_start|date:"Y-m-d":id.date_is_all_day }}

An alternative is to display the “all day” date in the timezone of the resource itself:

\{\{ id.date_start|date:”Y-m-d”:id \}\}

Date formatting characters

Date uses the same format as PHP’s date function with some extensions and some omissions.

All supported formatting characters are listed below:

CharacterDescriptionExample output
a“a.m.” or “p.m.” (note that this follows Associated Press style and adds periods).“a.m.”
AUppercase “AM” or “PM”.“AM”
bMonth, textual, in three lowercase characters.“jan”
cISO-8601 date format.“2004-02-12T15:19:21+00:00”
dDay of the month in two digits with leading zero, i.e. “01” to “31”.“01”
DDay of the week, textual, three letters of which the first one uppercase.“Mon”, “Fri”
eShow era when date is BCE (Before Common Era, so before the year 1).“BCE”
EAlways show era.“BCE”, “CE”
fIf minutes is zero then show only the hour, otherwise the hour and the minutes. Hours are shown using the “g” format character.“2”, “3:01”
FMonth, textual, full english name with first character in uppercase.“January”
g12 Hour format without leading zero, i.e. “1” to “12”.“1”
G24 Hour format without leading zero, i.e. “0” to “23”.“0”, “15”
h12 Hour format with leading zero, i.e. “01” to “12”.“01”
H24 Hour format with leading zero, i.e. “00” to “23”.“00”, “15”
iMinutes with leading zero, i.e. “00” to “59”.“00”, “46”
iDaylight saving time flag. “1” if DST is in effect, “0” if no DST.“0”
jDay of the month without leading zero, i.e. “1” to “31”.“1”, “28”
l(lowercase L) Day of the week, textual, full english name with first character in uppercase.“Monday”, “Friday”
LBoolean for whether the year is a leap year. Returns the string “True” or “False”.“True”, “False”
mMonth with leading zero, i.e. “01” to “12”.“01”, “12”
MMonth, textual, in three characters, first character in uppercase.“Jan”
nMonth without leading zero, i.e. “1” to “12”.“1”, “12”
NMonth abbreviation in Associated Press style. March, April, June and July are shown in full. September as “Sept.” and all other months as three letter abbreviations with a full stop appended.“Jan.”, “June”, “Sept.”, “Dec.”
ODifference to Greenwich Mean Time (GMT).“+0200”
PTime in 12 hour format with minutes and “a.m.” or “p.m.” appended. Minutes are left off if they are zero, and the strings “midnight” or “noon” if appropriate.“1 a.m.”, “noon”, “1:30 a.m.”, “12:30 p.m.”
rRFC 2822 formatted date.“Thu, 21 Dec 2000 16:01:07”
sSeconds with leading zero.“09”
SEnglish ordinal suffix for the day of the month, 2 characters; i.e. “st”, “nd”, “rd” or “th”.“st”, “nd”
tNumber of days in the given month, i.e. “28” to “31”.“30”
TTimezone used for displaying the date.“CEST”
USeconds since the Unix epoch of January 1, 00:00:00 GMT.1254911050
wDay of the week, numeric. 0 For sunday to 6 for saturday.“0”, “6”
WISO-8601 week number of the year, starting on mondays.“22”
xYear in (at least) four digits, in accordance with ISO 8601.“0313”, “-0500”, “2010”
yYear in two digits.“01”, “99”
YFull year. BCE years are shown as a positive number. Use e or E to add the era.“1999”, “2010”, “313”
zDay of the year, i.e. 1 to 366.“361”

To construct a date in a template, the filter also accepts Erlang lists as input, so the following will work:

{{ [1990,10,10]|date:"j F Y" }}

Will output 10 October 1990. This also works with datetimes:

{{ [[1990,10,10],[10,11,12]]|date:"j F Y - H:i:s" }}

Will output 10 October 1990 - 10:11:12.

Edit on GitHub

add_year Dates date_range

Referred by

rsc

See also