How to get formatted DateTimeField value outside of a Django template?

I declared DateTimeField in my model.py

date = models.DateTimeField(db_index=True, auto_now_add=True)

I show this value in my template as follows:

{{comment.date}}

and this is how it is displayed:

Feb. 9, 2014, 2:18 p.m.

Apparently, the django template engine formats DateTimeField automatically because the server returns this data with the following values:

2014-02-09 14:18:54.721021+00:00

But how can I do to return this value generated from the server?

I am making an AJAX call to the server and the date it naturally returns without format. Since I have a javascript file outside of the template, I cannot just do it {{comment.date}}.

+3
source share
2 answers

there may be several different answers, and each of them will have pros and cons.

API, API.

-, (json), , .

JSON , ISO-8601, javascript, . agular.js - , moment.js( ) .

, imho - iso-8601.

, json.dump.

+1

, , , , , , , .

. Python datetime strftime. Datetime , 2014-02-09 14: 18: 54.721021 + 00: 00 9 2014 ., 2:18 .

formatted_datetime_as_string = comment.date.strftime('%b. %d, %Y, %H:%M %p')

formatted_datetime_as_string JSON.

-1

All Articles