How to format datetime stamp in JS like it is done by the date template tag?

34 views
Skip to first unread message

the.one.abo...@gmail.com

unread,
Mar 21, 2024, 7:17:18 AM3/21/24
to Django users
Hello everyone, 

I am customizing Django admin by adding a datetime stamp to the page using JavaScript. I want to format the datetime stamp according to the format set by Django. 

It is possible to get the DATETIME_FORMAT using the get_format() method in JS which is "'j M Y, P'". But, this format is not supported by the Date().strftime method. 

I don't want to hard-code the formatting of the datetime string. I want the code to adapt according to the Django settings for LANGUAGE_CODE, USE_I18N, and USE_L10N settings.

TIA! 

Regards,
Gagan Deep

Ryan Nowakowski

unread,
Mar 23, 2024, 9:59:01 AM3/23/24
to django...@googlegroups.com
You can format the data including the datetime as a JSON string in a script tag via the normal template stuff. Then read that from JavaScript.
Reply all
Reply to author
Forward
0 new messages