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" }}
When mydate is {2009,6,1} this returns 2009-06-01 as output.
See also the timesince filter to display a human readable relative time like 10 hours ago.
Date uses the same format as PHP’s date function with some extensions and some omissions.
All supported formatting characters are listed below:
| Character | Description | Example output |
|---|---|---|
| a | “a.m.” or “p.m.” (note that this follows Associated Press style and adds periods) | “a.m.” |
| A | Uppercase “AM” or “PM” | “AM” |
| b | Month, textual, in three lowercase characters. | “jan” |
| c | ISO-8601 date format | “2004-02-12T15:19:21+00:00” |
| d | Day of the month in two digits with leading zeros, i.e. “01” to “31” | “01” |
| D | Day of the week, textual, three letters of which the first one uppercase. | “Mon”, “Fri” |
| f | If 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” |
| F | Month, textual, full english name with first character in uppercase. | “January” |
| g | 12 Hour format without leading zero, i.e. “1” to “12”. | “1” |
| G | 24 Hour format without leading zero, i.e. “0” to “23” | “0”, “15” |
| h | 12 Hour format with leading zero, i.e. “01” to “12” | “01” |
| H | 24 Hour format with leading zero, i.e. “00” to “23” | “00”, “15” |
| i | Minutes with leading zero, i.e. “00” to “59” | “00”, “46” |
| j | Day of the month without leading zeros, 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” |
| L | Boolean for whether the year is a leap year. Returns the string “True” or “False”. | “True”, “False” |
| m | Month with leading zeros, i.e. “01” to “12” | “01”, “12” |
| M | Month, textual, in three characters, first character in uppercase. | “Jan” |
| n | Month without leading zeros, i.e. “1” to “12” | “1”, “12” |
| N | Month 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.” |
| O | Difference to Greenwich Mean Time (GMT). | “+0200” |
| P | Time 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.” |
| r | RFC 2822 formatted date. | “Thu, 21 Dec 2000 16:01:07 |
| S | English ordinal suffix for the day of the month, 2 characters; i.e. “st”, “nd”, “rd” or “th” | “st”, “nd” |
| t | Number of days in the given month, i.e. “28” to “31” | “30” |
| U | Seconds since the Unix epoch of January 1, 00:00:00 GMT. | 1254911050 |
| w | Day of the week, numeric. 0 For sunday to 6 for saturday. | “0”, “6” |
| W | ISO-8601 week number of the year, starting on mondays. | “22” |
| y | Year in two digits. | “01”, “99” |
| Y | Year in four digits. | “1999”, “2010” |
| z | Day 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.
See also