soubhagya
soubhagya

Reputation: 806

How to convert django default timezone to different format

Time format

"2018-12-13T05:20:06.427Z"

django providing time zone in above format when i am fetching data from database using ORM query.

In my model field is in below way.

models.DateTimeField(auto_now=True, blank=True,null=True)

How can i convert it into "24 feb 2018" like this

Upvotes: 0

Views: 73

Answers (2)

uedemir
uedemir

Reputation: 1714

Apart from @Sosthenes Kwame Boame answer, you can use strftime for formatting.

import datetime

time = datetime.datetime.now().strftime('%d %b %Y') 

Out[13]: '13 Dec 2018'

Instead of passing datetime module to time variable, you should pass your model field's value.

If you want to learn more about format type then you can visit the documentation page.

Upvotes: 4

Sosthenes Kwame Boame
Sosthenes Kwame Boame

Reputation: 373

I am guessing you want to display this on the frontend?

You need to do this in your template:

{{ object_name.datefield_name|date:'j b Y' }}

So you call the datefield and render it with the '|date' tag with a format assigned ':'FORMAT''.

Learn more about the date tag, along with the various formats here: https://docs.djangoproject.com/en/2.1/ref/templates/builtins/#date

Upvotes: 1

Related Questions