Django template datetime.weekday name - python

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'))

Related

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"

Does Django 1.7 has a automatic way to convert datetime fields from manual HTML form to model?

I'm writing a simple app that has a form with field who saves the birth_date with format 'd/m/Y'. Due the date format we use in Brazil I'm having to convert the date with the following code to match the format 'Y-m-d':
arr_date = request.POST['birth_date'].split('/')
author.birth_date = datetime.date(int(arr_date[2]), int(arr_date[1]), int(arr_date[0]))
The form's field is:
<input type="date" name="birth_date" value="{{ author.birth_date|date:'d/m/Y' }}"/>
Is there a "fancier" way to make this conversion?
You can use datetime.strptime() from standard python library:
from datetime import datetime
dt = datetime.strptime(str_date, '%d/%m/%Y')
catavaran answer works but also you can use the DJANGO_INPUT_FORMATS setting https://docs.djangoproject.com/en/1.7/ref/settings/#date-input-formats , it will benefit you if you want to accept many date formats or if you are using several forms in your app.
If you want to display the date you can benefit of https://docs.djangoproject.com/en/1.7/ref/settings/#date-format

How to display localized time from datetime in django template

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 }}

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.

django autoconverting datetime property

my datetime property is saving in mysql in this format 2011-03-17 00:00:00 but after fetchind the data with filter function it is giving March 17,2011 midnight but i have not say to do any this type of task. My question is how can i insist django to stic to show same value what is saved in MYSQL.
you'll want to use the datetime format, django's DateTimeField[1] really is a wrapper for datetime.datetime.
in the templates you can use the date[2] filter to apply the format you want for example:
{{ item.date|date:"Y-m-d H:i:s" }}
This should print out 2011-03-17 00:00:00 in the template. In views use datetimes.strftime[3]
[1] http://docs.djangoproject.com/en/dev/ref/models/fields/#datetimefield
[2] http://docs.djangoproject.com/en/1.2/ref/templates/builtins/#date
[3] http://docs.python.org/library/datetime.html#strftime-and-strptime-behavior
I have a feeling your database schema knows this is a date, right? In that case it's not being stored in the format you describe, but as some representation such as seconds since the era.
This means that when you retreave it your code has to do something with it to make it look right. If you don't tell it how to look it'll default to the format you see, but if you use strftime in your python code and a filter in your templates you can make it look however you like, including the original format how you saw it.
Of course the easy way out is to store it in the db as text...

Categories

Resources