0

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.

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:

[{"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. I'm mainly interested in converting the JSON string date to JavaScript date. Please note that since this is in Google Apps Script, importing libraries (i.e. Moment.js) is not possible.

Community
  • 1
  • 1
fragilewindows
  • 1,394
  • 1
  • 15
  • 26
  • your json looks like an array, if that's the case you can't use json.flightDate, you need to specify the index first json[1].flightDate – StackOverMySoul Sep 23 '16 at 16:20
  • Oops, you're right. I actually have it iterating in my script. Just forgot to put in the question. Editing it now. Thanks. – fragilewindows Sep 23 '16 at 16:24
  • why do you think you are doing anything wrong? It looks perfectly fine and working as expected. your `flightDate` is less than `afterDate` thats the only reason its not going in the if clause. – Deendayal Garg Sep 23 '16 at 16:25
  • I'm pushing the JSON to Google sheets and all of the dates on my flightDate are being changed to 1969-12-31. Can't figure out why that's happening. I'm unfamiliar with Google Apps Script and so when I push to the sheets, I don't know why it's changing it – fragilewindows Sep 23 '16 at 16:32
  • Note that given `new Date('2013-03-01')` then the Date will be treated as UTC by most implementations, but not all. It is generally recommended to not use the built–in parser, but to use your own function or a library so you can be sure the Date is parsed consistently everywhere. – RobG Sep 24 '16 at 03:32

2 Answers2

1

First of all, if that is a copy of your json then your parse will not work because there are missing , in it.

After correcting your json, then you can use the Arrays filter to get the flights after a certain date.

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);
knutesten
  • 594
  • 3
  • 16
0

EDIT: Now that JSON seems to be syntactically correct, I can only comment why did it print 1969-12-31.

EDIT2: According to code snippet you recently provided; (which has many syntax errors!) you are comparing flightDate string to afterDate Date. You should create a Date object from flightDate first, then compare accordingly.

When you call Date constructor with invalid values; most of the time it'll print out 1970-01-01 which is the epoch.

You are probably behind UTC timezone, so yours was 1969-12-31.

JuniorDev
  • 1,170
  • 2
  • 9
  • 19
  • Yeah I typed it out instead of copying and pasting since I had to change a lot of the values. I was in the middle of editing the post when you replied (people on here are fast :-) but it's now displaying as what I have running on my machine. Your explanation for the time printout is helpful. Thanks. – fragilewindows Sep 23 '16 at 16:42
  • 1
    "*Date constructor with invalid values; most of the time it'll print out 1970-01-01…*" that statement is not supported by [*the language specification*](http://ecma-international.org/ecma-262/7.0/index.html#sec-date.parse). If the string appears to conform to the limited subset of ISO 8601 supported by the standard and one or more values are invalid (e.g. 2016-09-43), the result should be an invalid date (i.e. one with a time value of NaN). However, it might also be treated as not ISO 8601, in which case the result is implementation dependent. – RobG Sep 24 '16 at 03:35