最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Within Django, how can I turn my Python datetime string into a Javascript Date object? - Stack Overflow

programmeradmin1浏览0评论

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.

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.

Share Improve this question edited Jan 14, 2017 at 13:37 Jay Jung asked Jan 14, 2017 at 11:58 Jay JungJay Jung 1,8953 gold badges25 silver badges53 bronze badges 4
  • If you need to do js data manipulation, I would strongly remend you to use DRF django-rest-framework – Wtower Commented Jan 14, 2017 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 Commented Jan 14, 2017 at 12:13
  • all you will receive are strings, so you have to convert it to Date manually. See this question & answer for example: stackoverflow./questions/5619202/… – yedpodtrzitko Commented Jan 14, 2017 at 12:42
  • @Simonster I cannot. In order to use it you need to invest time. My remendation 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 Commented Jan 14, 2017 at 13:46
Add a ment  | 

2 Answers 2

Reset to default 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.

# 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/2/library/datetime.html#strftime-strptime-behavior

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论