将字符串(从JSON解析)转换为javascript date对象有困难。

时间:2021-09-29 22:26:13

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

我正在尝试用以下方法创建javasscript date对象。

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

哪个抛出同样的错误?

3 个解决方案

#1


3  

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.

试试这个新的日期(“2013 - 07 - 08 - t19:00:00”)。你的时间似乎是在必要的格式,所以不应该有问题。

#2


0  

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

它抛出一个错误,因为js是大小写敏感的,而且没有“日期”对象。你应该使用

var tempDate = new Date(object.start_time);

#3


0  

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

在上面的代码中,您没有尝试创建date对象。要创建date对象,您需要使用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);

#1


3  

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.

试试这个新的日期(“2013 - 07 - 08 - t19:00:00”)。你的时间似乎是在必要的格式,所以不应该有问题。

#2


0  

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

它抛出一个错误,因为js是大小写敏感的,而且没有“日期”对象。你应该使用

var tempDate = new Date(object.start_time);

#3


0  

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

在上面的代码中,您没有尝试创建date对象。要创建date对象,您需要使用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);