How to display localized time from datetime in django template - python

To be brief.
next_contact is a UTC datetime
When I write in template
{{ event.next_contact }}
I get properly localized date and time
But in this place I want output only time so I do:
{{ event.next_contact.time }}
and then I get non localized time
How to fix it??

Use {{ event.next_contact|localtime|date:"H:i" }}. If you have set USE_TZ=True you can skip the localtime filter.
As of Django 1.7 when passed a datetime value with attached timezone information (a time-zone-aware datetime instance) the time filter will accept the timezone-related format specifiers 'e', 'O' , 'T' and 'Z'.

Instead of using the attribute, just use the time filter on the datetime:
{{ event.next_contact|time }}

Related

Jinja - email template, date time formatting with a variable

This is driving me crazy, feels like it should be easier.
Using Jinja code for a HTML email, in a drag and drop editor - so no access to the <head>!
I have a date I’ve set as a variable the date format is YYYY-MM-DD HH:MM:SS
I want to change the format using datetimeformat function, it works fine as {{ today()|datetimeformat("%y-%m-%d") }} - but I'll be buggered if I can exchange today() with my variable.
{% set bonusBeforeDate = linkedData.accessCodes[userAttribute['ReferralSource']]['promotion']['rules']['member_joined_date']['before'] %}
And then
{{ bonusBeforeDate|datetimeformat("%y-%m-%d") }}
OK, someone very kind has helped me out with this one, so I'll share it here in the hope it saves anyone else the headache later.
The reason it is not working is because datetimeformat requires datetime object, while yours is a string. The today() function returns datetime object, so it works directly. You will have to perform some transformations to get this working. The following sequence needs to be followed:
Convert your string to another suitable string to represent datetime object. Not all date looking strings can be converted to datetime object, they need to follow a specific format.
Now convert the new string to a datetime object (strtotime)
Apply any datetime formatting to the date time object (datetimeformat)
In practical terms with your example:
Step 1:
{% set myDate='2020-12-31 12:59:00' %}
{% set myDateFormatted = myDate|split(' ',2) %}
{% set myDateFinalFormat = myDateFormatted[0]+'T'+myDateFormatted[1]+'Z' %}
The value of myDateFinalFormat at this point should be 2020-12-31T12:59:00Z which is the exact string format we are interested in.
Steps 2 and 3:
{{ myDateFinalFormat|strtotime("yyyy-MM-dd'T'HH:mm:ssXXX")|datetimeformat("%y-%m-%d") }}
This will output: 20-12-31

Datetime in django with template filter will show different value

I'm using Django template filters to format a datetime instance, but it looks like its value differs from raw date in the datebase.
I used datetime.now() in Python to create the instance.
{{ modeltest.date }}
will show
"Jan. 3, 2019, 5:27 a.m."
on the page, but
{{ modeltest.date | date:"Y-m-d H:m:s" }}
displays
"2019-01-03 05:01:19"
Note the difference of the minutes displayed (5:27 vs 05:01)
I'm getting a little confused here.
Does anyone know this?
You mixed up the date filters. You used the month twice (lowercase m) instead of using minutes (lowercase i).
You are seeing "2019-01-03 05:01:19" (with the minutes as 01 instead of 27) because it actually show the month (January is the first month, so 01).
You can change this
date:"Y-m-d H:m:s"
to
date:"Y-m-d H:i:s"

Django template datetime.weekday name

Is there a way to display the weekday of a datetime object in a template as the actual name of the weekday? Basically I want it to print Friday instead of 5.
See the documentation for the built-in date filter. From there you'll see you need to use:
l Day of the week, textual, long. 'Friday'
For clarity's sake, the template tag format character "l" (lower-case "L") can be used in the Django template like so:
{{ object.some_date_field | date:"l" }}
Django 1.8.2
You can also use:
import datetime
today = datetime.datetime.today()
print(today.strftime('%A'))

How to display "This many months ago" in Django using Humanize?

I have this variable:
{{ video.pub_date }}
which outputs:
May 16, 2011, 2:03 p.m.
How can I get it to show:
1 month ago
I have loaded humanize on the page, but the django docs for humanize doesn't really show how to implement it to show what I want:
https://docs.djangoproject.com/en/dev/ref/contrib/humanize/
It just says that it's possible at the bottom of the page there.
You have to have the development version of Django to use the naturaltime filter
{% load humanize %}
{{ video.pub_date|naturaltime }}
Humanize isn't a template tag, it's a library of template filters which means when you load it you have access to various other filters included in the module.
Alternatively you can use the timesince filter withouth having to load any other template tag libraries
{{ video.pub_date|timesince }}
{{ video.pub_date|timesince }} ago
From the docs at timesince
timesince
Formats a date as the time since that
date (e.g., "4 days, 6 hours").
Takes an optional argument that is a
variable containing the date to use as
the comparison point (without the
argument, the comparison point is
now). For example, if blog_date is a
date instance representing midnight on
1 June 2006, and comment_date is a
date instance for 08:00 on 1 June
2006, then {{
blog_date|timesince:comment_date }}
would return "8 hours".
Comparing offset-naive and
offset-aware datetimes will return an
empty string.
Minutes is the smallest unit used, and
"0 minutes" will be returned for any
date that is in the future relative to
the comparison point.
To activate what filter. adds 'django.contrib.humanize' to your INSTALLED_APPS setting,
Once you’ve done that,
use {% load humanize %} in a templates, and you’ll have access to the follow filters,
{{comment.timestamp | naturaltime }}

Convert string to datetime in django?

i want to display time in 12 hour format in django templates as iam storing time data in a charecter eg('18:30:00') field how to convert this string time data to date in django/python?
If you absolutely must do this, use time.strptime. It would be a better idea not to store structured data in a string if you can avoid it, though.
Within Django's templates you can use the date Template Filter:
{{ value|date:"h:i:s a" }} # '18:30:00' would appear as 6:30:00 p.m.
{{ value|date:"g:i:s a" }} # '18:30:00' would appear as 06:30:00 p.m.
value would be the string variable you pass in the Context to the Template when you render it. The string appearing after date: formats the time.
You can find more here: http://docs.djangoproject.com/en/1.3/ref/templates/builtins/#date
Hank Gay's answer is more correct than mine; you should avoid keeping structured data in a string if at all possible.

Categories

Resources