0

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

Sham Dhiman
  • 1,348
  • 1
  • 21
  • 59
soubhagya
  • 788
  • 2
  • 12
  • 37

2 Answers2

4

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.

uedemir
  • 1,654
  • 1
  • 12
  • 21
1

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