1

I am attempting to go from JSON data -> Javascript Date object within Django.

Currently, I have it set up so that I have a function which hits the API and saves the JSON data into one of my Model objects.
edit: the JSON datetime string looks like so: '2017-01-14 14:00:00'

Then in my View, I will query for the object containing the JSON, and send a datetime string which was acquired from the JSON, over to my template as a context variable.

Within the template, I am trying to graph the string data using Google Chart, and Google Charts requires that the first column for the Line graph to be a JS Date object. How can I turn the sent over template variable: {{ date }} into a format equivalent to Javascript's

new Date(2017, 01, 14)  

so that it may be used with Google Charts?

edit: The problem is not that I don't know how to format strings, but that I don't know how to get the string to appear in the first place, as it will be a django template variable first.

Jay Jung
  • 1,805
  • 3
  • 23
  • 46
  • If you need to do js data manipulation, I would strongly recommend you to use DRF http://www.django-rest-framework.org/ – Wtower Jan 14 '17 at 12:00
  • @Wtower my understanding was that DRF was used to create your own API. Can you point me to a specific area/topic within DRF that I can read up on regarding what I am trying to solve? – Jay Jung Jan 14 '17 at 12:13
  • all you will receive are strings, so you have to convert it to `Date` manually. See this question & answer for example: http://stackoverflow.com/questions/5619202/converting-string-to-date-in-js – yedpodtrzitko Jan 14 '17 at 12:42
  • @Simonster I cannot. In order to use it you need to invest time. My recommendation is not specific to your problem, for that matter sdolan's answer works fine. But if you plan to have extensive json queries to your back-end then drf is a must imo. – Wtower Jan 14 '17 at 13:46

2 Answers2

5
new Date({{ value|date:"U" }} * 1000)  

Converts Django template variable datetime object into a unix timestamp which is an acceptable argument for creating JS Date objects.
Multiply by 1000 to retrieve seconds value.

Jay Jung
  • 1,805
  • 3
  • 23
  • 46
  • Genius indeed!! I just wasted two days of my life trying to figure this out! Thanks! – thenextmogul Nov 02 '19 at 00:07
  • 1
    Where does the Date object come from? Is this Python or JS code? – Lenka Pitonakova Mar 04 '20 at 19:03
  • 1
    It would be really helpful if you could explain your code better - what is `value`? What is `date`? Using this code and trying to substitute both with what is returned by Django but the code doesn't run – Lenka Pitonakova Mar 04 '20 at 19:08
  • @Elendurwen value is the template variable and date is the django template filter. So in this case value is the variable name you pass from python into the html or javascript file. And date is just a builtin-django filter, so need to import or have any libraries to use it. – theQuestionMan Sep 08 '20 at 03:19
  • @Elendurwen Here is an example: **Main.py:** `todays_date = datetime.datetime.today().date() render_with_rc('timesheet/roster.html', request, today_date)` **MyPage.html:** `` – theQuestionMan Sep 08 '20 at 03:21
2
# general python datetime stringformatting:

>>> import datetime
>>> d = datetime.datetime.now()
>>> d.strftime("%Y-%m-%d %H:%M:%S")
'2017-01-14 05:04:26'

# you can embed other chars to fake the js date constructor
>>> d.strftime("new Date(%Y, %m, %d)")
'new Date(2017, 01, 14)'

See the docs @ https://docs.python.org/2/library/datetime.html#strftime-strptime-behavior

Sam Dolan
  • 31,966
  • 10
  • 88
  • 84