Hello, thanks for your reply! The DATETIME_FORMAT setting of Django uses format strings that are incompatible with JS. E.g the default DATETIME_FORMAT value in Django is 'N j, Y'.
You can check the allowed format strings in Django documentation <https://docs.djangoproject.com/en/5.0/ref/templates/builtins/#std-templatefilter-date> . - Gagan Deep On Thursday, March 21, 2024 at 7:28:29 PM UTC+5:30 Matthias Yeni wrote: > To format the timestamp in JavaScript according to the format defined by > Django, you can use Django settings to determine the format and adapt it in > JavaScript. Here's how you can do it: > > > > Retrieve the date and time format defined by Django in your JavaScript > file using a Django template rendering method. > > > > > > <script> > > // Get the date and time format defined by Django > > var dateTimeFormat = "{{ DATETIME_FORMAT }}"; > > </script> > > > > Use the Moment.js JavaScript library to format the timestamp based on the > format retrieved. > > > > <script src=" > https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js > "></script> > > <script> > > // Convert current date and time using Moment.js > > var currentDateTime = moment(); > > > > // Format date and time using format retrieved from Django > > var formattedDateTime = currentDateTime.format(dateTimeFormat); > > > > // Use the formattedDateTime variable wherever you want to display the > timestamp in your page console.log("Horodatage formaté :", > formattedDateTime); > > </script> > > > > With this approach, your JavaScript code will automatically adapt to the > date and time format set by Django based on the LANGUAGE_CODE, USE_I18N, > and USE_L10N parameters, without needing to hardcode the formatting of the > datetime string into your JavaScript code. > > Le jeu. 21 mars 2024 à 12:13, [email protected] < > [email protected]> a écrit : > >> Hello, >> >> I have 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. But, this format is not supported by the Date().srftime 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 >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Django users" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to [email protected]. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/django-users/cab2283d-c0dd-40d2-a82e-6a5dce675038n%40googlegroups.com >> >> <https://groups.google.com/d/msgid/django-users/cab2283d-c0dd-40d2-a82e-6a5dce675038n%40googlegroups.com?utm_medium=email&utm_source=footer> >> . >> > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/1658b2b6-7773-4b25-a3cd-8391eb6c690cn%40googlegroups.com.

