Having trouble converting string (parsed from JSON) into javascript date object

烂漫一生 提交于 2019-12-24 03:09:07

问题


I am trying to create javasscript date object in the following way

var object = {"name":"Bay Area Global Health Film Festival","start_time":"2013-07-08T19:00:00","end_time":"2013-07-08T23:45:00","timezone":"America/Los_Angeles","location":"San Francisco","id":"458415670892007","rsvp_status":"attending"}

var tempDate = date(object.start_time);

And I'm getting back the error:

date is not defined 

I have also tried trimming the string using:

var tempDate = date(object.start_time.slice(0,object.start_time.indexOf("T"));
//This yields an input of 2013-07-08

Which throws the same error


回答1:


Try this new Date("2013-07-08T19:00:00"). The time you are gettng seems to be in the required format so there shouldn't be issues.




回答2:


It throws an error because js is case sensitive and there is no 'date' object. You should use

var tempDate = new Date(object.start_time);



回答3:


In the above code you are not trying to create date object. To create date Object you need to use new.

There are four ways of instantiating a date object.

var d = new Date();
var d = new Date(milliseconds);
var d = new Date(dateString);
var d = new Date(year, month, day, hours, minutes, seconds, milliseconds);


来源:https://stackoverflow.com/questions/17269435/having-trouble-converting-string-parsed-from-json-into-javascript-date-object

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