Parse a Python datetime string in Javascript

為{幸葍}努か 提交于 2020-01-23 01:29:05

问题


I need to parse a Python-generated datetime string into a Javascript Date object. I went the simplest route:

In Python:

dstring = str(mydate)

example dstring = '2012-05-16 19:20:35.243710-04:00'

In Javascript (with datejs library):

d = new Date(dstring);

This gets me a correct date object in Chrome, but an "Invalid Date" error in Firefox and Safari (on Mac).


回答1:


You need to parse the string into a JS Date. The Date constructor is not enough. Maybe you should consider using a library such as datejs:

http://www.datejs.com/

Datejs extends the Date object with useful methods such as:

Date.parse('Thu, 1 July 2004 22:30:00');

Needless to say that date/time formats are customizable.



来源:https://stackoverflow.com/questions/10628169/parse-a-python-datetime-string-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!