user2086641
user2086641

Reputation: 4371

display date and time in django

template.html

{{incident.created_date_time}}

This is ahowing the date and time as June 26, 2013, 5:42 p.m. ,how to show it in June 26 2013 at 5:42 P.M..

How to use template filter to convert the date and time.

Thanks

Upvotes: 0

Views: 5411

Answers (3)

binarym
binarym

Reputation: 367

You can also do it "app-wide" by setting DATE_FORMAT or DATETIME_FORMAT to your settings.py.

See https://docs.djangoproject.com/en/1.8/ref/settings/#date-format

Upvotes: 0

Silwest
Silwest

Reputation: 1620

Try this:

{{ incident|date:"F d Y P" }}

https://docs.djangoproject.com/en/dev/ref/templates/builtins/#date

Edit: No if you want to have at in date you need to write your own date filter.

Look at limelights post.

Upvotes: 2

Henrik Andersson
Henrik Andersson

Reputation: 47222

The Django builtin template filter won't really do this exact formatting for you but a close second using it would be

{{ incident.created_date_time|:"F j Y P" }}

Otherwise you would have to create your own date filter.

@register.filter(name='myDate')
def myDate(value, arg):
    #arg is optional and not needed but you could supply your own formatting if you want.
    dateformatted = value.strftime("%b %d, %Y at %I:%M %p")
    return dateformatted

This requires the incident.created_date_time to be a datetime object.

and then you'd use it like

{{ incident.created_date_time|myDate }}

Upvotes: 1

Related Questions