This section discusses using filters to display and format date and time variables inside the template files. You probably will work with dates to either display the current date or display dates that are fields in a model.
The following sections describe the date, time, now, timesince, and timeuntil filters and how to apply them in a template.
The date filter can be applied to Python date variables, including date fields in a model object. The date filter accepts a string of format characters as an argument and formats the date based on those characters. For a full description of the datetime format characters, refer to Appendix C, "Formatting Dates and Times."
The following examples should help you understand how the date filter works. The entryDate variable is a date instance with a value of February 7, 2005.
The following code applies the j character to render the day of the week without the leading 0, the S character to render the English day/month suffix, the F character to render the month as textual, and the Y character to render the four-digit year:
{{ entryDate|date:"jS F Y" }} renders 7th February 2005
The following code applies the D character to render the three-character day-of-the-week text, the F character to render the month as textual, the j character to render the day of the week without the leading 0, the S character to render the English day/month suffix, and the Y character to render the four-digit year:
{{ entryDate|date:"D, F jS, Y" }} renders Mon, February 7th, 2005
The following code applies the m character to add the two-digit month value, the d character to add the two-digit day value, and the Y character to add the four-digit year:
{{ entryDate|date:"m/d/Y" }} renders 02/07/2005
Did you Know?
Did you notice that the comma and slash characters that were added to the format string for the date filter get rendered with the values specified by the format characters?
The time filter can be applied to Python date and datetime variables, including datetime fields in a model object, the same way that the date filter is. The time filter also accepts a string of format characters as an argument and formats the time based on those characters. For a full description of the datetime format characters, refer to Appendix C.
By the Way
You can use only the characters that format time specifically in the time filter. The date characters will not work.
The following examples should help you understand how the time filter works. The someTime variable is a time instance with a value of 10:51 p.m. MDT.
The following code applies the h character to render the two-digit hour in 12-hour format, the i character to render the two-digit minutes value, the a character to render the a.m./p.m. text string, and the T character to render the timezone string:
{{ someTime|date:"h:ia T" }} renders 10:51p.m. Mountain Daylight Time
The following code applies the H character to render the two-digit hour in 24-hour format, the i character to render the two-digit minutes value, the s character to render the two-digit seconds value, the a character to render the AM/PM text string, and the O character to render the offset to Greenwich time:
{{ someTime|date:"H:i:sAO" }} renders 22:51:46PM+1800
Did you Know?
Did you notice that the colon characters that were added to the format string for the time filter get rendered with the values specified by the format characters?
Because we are discussing the date and time filters in this section, it would be a good idea to discuss the now tag as well. The now tag works similarly to the date and time filters in that it accepts a string of format characters to format the current date and time. However, the now tag uses tag syntax instead of filter syntax and is not applied to a variable object. The following line of code shows how to render the current date and time in a template using the now filter:
{% now "M d, Y g:ma" %}
This line of code renders the date and time in the following format:
Jan 17, 2008 3:01p.m.
For a full description of the datetime format characters, refer to Appendix C.
The timesince filter can be applied to either a time or datetime variable. It accepts an optional time or datetime object as the only argument. The timesince filter determines the time since the time or datetime variable to the current time and renders a textual string noting the difference.
The timeuntil filter can also be applied to either a time or datetime variable. It also accepts an optional time or datetime object as the only argument. However, the timeuntil filter subtracts the current time from the time or datetime variable and renders a textual string noting the delta.
If a time or datetime argument is added, the timesince and timeuntil filters determine the time elapsed between the filter argument and the variable, and render a textual string noting the difference.
Watch Out!
Currently, the time or datetime argument should be earlier than that of the original variable in both the timeuntil and timesince filters. This was different than I expected. I'm not certain if this will change in the future.
The timesince and timeuntil filters measure the difference in units of years, months, days, minutes, and seconds. However, only the two largest units are displayed, such as years and months, months and days, days and hours, hours and minutes, and minutes and seconds.
The following code snippet shows an example of using the timeuntil and timesince filters to display textual information about the last archive date, next archive date, and current date and time:
<h1>Archive Data</h1> <table border=1 padding=2> <tr> <td>Current Time</td> <td>Last Archive</td> <td>Next Archive</td> </tr> <tr> <td>{% now "M d, Y h:m:sa" %}</td> <td>{{ lastArch|date:"M d, Y h:m:sa" }}</td> <td>{{ nextArch|date:"M d, Y h:m:sa" }}</td> </tr> </table> <p> It has been {{ lastArch|timesince }} since the last archive.<p> The next archive will take place in {{ nextArch|timeuntil }}.<p> The time between the archives will be {{ nextArch|timeuntil:lastArch }}.
Figure 9.7 shows the output of the code in the web browser.
Try It Yourself: Format Date and Time Variables in a TemplateIn this section, you will add a datetime field to the Blog model in the Person application. You will use the date formatting filters to display the dates correctly in the Person details view. Follow these steps to add the datetime field to the Blog model and update the person_details.py template:
http://127.0.0.1:8000/People/Info/1/ Listing 9.5 shows the complete person_details.py file. Listing 9.5. Full Contents of iFriends/templates/Person/person_details.py
|