简体   繁体   English

将JSON字符串日期转换为JavaScript(Google Apps脚本)

[英]Convert JSON string date to JavaScript (Google Apps Script)

I am trying to convert a JSON string date (in Google Apps Script) to JavaScript date object and then check to see if that date is after another date but I can't get it to work. 我正在尝试将JSON字符串日期(在Google Apps脚本中)转换为JavaScript日期对象,然后检查该日期是否在另一个日期之后,但是我无法使其正常工作。

I tried using the suggestion here . 我在这里尝试使用建议。 but my output is incorrect. 但我的输出不正确。 What am I doing wrong? 我究竟做错了什么? Thanks for your help. 谢谢你的帮助。


Code Snippet: 代码段:

var json = JSON.parse(resp);

var flightDate = new Date(json[i].flightDate)

for (i = 0; i < json.length; i++ {

flightDate = json[i].flightDate
traveler = json[i].traveler
flight = json[i].flight
destination = json[i].destination

var afterDate = new Date('1997-07-30')

if (flightDate >= afterDate) {
    output.push ([flightDate, traveler, flight, destination])
    }
}

Output: 输出:

1969-12-31

JSON: JSON:

[{"flightDate": "2013-03-01",
"traveler": "Paul Carter",
"flight": "JetBlue",
"destination": "Australia"
},
{"flightDate": "1997-02-18",
"traveler": "Paul Carter",
"flight": "American Airlines",
"destination": "Australia"
},
{"flightDate": "2004-05-25",
"traveler": "Paul Carter",
"flight": "JetBlue",
"destination": "Chile"
},
{"flightDate": "1995-08-05",
"traveler": "Paul Carter",
"flight": "United",
"destination": "Ireland"
}]

UPDATE: 更新:

JSON in this question was updated to accurately reflect what I have on my computer so the issue isn't the JSON formatting. 此问题中的JSON已更新,以准确反映我在计算机上拥有的内容,因此问题不是JSON格式。 I'm mainly interested in converting the JSON string date to JavaScript date. 我主要对将JSON字符串日期转换为JavaScript日期感兴趣。 Please note that since this is in Google Apps Script, importing libraries (ie Moment.js) is not possible. 请注意,由于这是在Google Apps脚本中,因此无法导入库(即Moment.js)。

First of all, if that is a copy of your json then your parse will not work because there are missing , in it. 首先,如果这是你的JSON的副本,那么您解析不会因为缺少工作,在里面。

After correcting your json, then you can use the Arrays filter to get the flights after a certain date. 更正json之后,您可以使用Arrays过滤器来获取特定日期之后的航班。

 var data = [{ flightDate: "2013-03-01", traveler: "Paul Carter", flight: "JetBlue", destination: "Australia" }, { flightDate: "1997-02-18", traveler: "Paul Carter", flight: "American Airlines", destination: "Australia" }, { flightDate: "2004-05-25", traveler: "Paul Carter", flight: "JetBlue", destination: "Chile" }, { flightDate: "1995-08-05", traveler: "Paul Carter", flight: "United", destination: "Ireland" }]; var afterDate = new Date("2000-01-01"); var flightsAfter = data.filter(function(flight) { return new Date(flight.flightDate) >= afterDate; }); console.log(flightsAfter); 

EDIT: Now that JSON seems to be syntactically correct, I can only comment why did it print 1969-12-31. 编辑:既然JSON在语法上似乎是正确的,我只能评论为什么打印1969-12-31。

EDIT2: According to code snippet you recently provided; EDIT2:根据您最近提供的代码片段; (which has many syntax errors!) you are comparing flightDate string to afterDate Date . (有很多语法错误!)您正在将flightDate string与afterDate Date进行比较。 You should create a Date object from flightDate first, then compare accordingly. 您应该首先从flightDate创建一个Date对象,然后进行相应比较。

When you call Date constructor with invalid values; 当您使用无效值调用Date构造函数时; most of the time it'll print out 1970-01-01 which is the epoch. 在大多数情况下,它将打印出1970-01-01的纪元。

You are probably behind UTC timezone, so yours was 1969-12-31. 您可能落后于UTC时区,所以您的时间是1969-12-31。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM