Skip to main content
PrintHouse
Inspiring
March 17, 2023
Answered

Strange parsing of Date object

  • March 17, 2023
  • 1 reply
  • 916 views

Hello,

I need help understanding how InDesign parses a Date object.

I'm feeding my script with a holiday list in form of a JSON object (generated by an external library, so I can't change it):

data = [
  {
    "date":"2023-03-19 00:00:00",
    "start":"2023-03-19T00:00:00.000Z",
    "end":"2023-03-20T00:00:00.000Z",
    "name":"Mother's Day",
    "type":"observance",
    "rule":"easter -21"
  },
  {
    "date":"2023-04-09 00:00:00",
    "start":"2023-04-08T23:00:00.000Z",
    "end":"2023-04-09T23:00:00.000Z",
    "name":"Easter Sunday",
    "type":"observance",
    "rule":"easter"
  },
  {
    "date":"2023-06-18 00:00:00",
    "start":"2023-06-17T23:00:00.000Z",
    "end":"2023-06-18T23:00:00.000Z",
    "name":"Father's Day",
    "type":"observance",
    "rule":"3rd sunday in June"
  }
];

But when I try to extract the date, I'm getting messed up results:

for (var i = 0; i < data.length; i++) {
	var dx = new Date(data[i].date),
	dm = dx.getMonth(),
	dd = dx.getDate(),
	dn = data[i].name,
	dt = data[i].type;
	alert("date: " + dx);
}

Namely:

Sat Jan 12 1924 00:00:00 GMT+0100
Sun Oct 12 1924 00:00:00 GMT+0100
Thu Nov 12 1914 00:00:00 GMT+0100

It doesn't make any sense! Why?

This topic has been closed for replies.
Correct answer rob day

Thanks. What is "moment()"? Is it an internal function or some external lib?

I'm using

var dm = parseInt(data[i].date.substr(5, 2), 10);
var dd = parseInt(data[i].date.substr(8, 2), 10);

for now and it works fine. Still would like to know why Date behaves like the above. 


Still would like to know why Date behaves like the above

 

Hi @PrintHouse , I think it’s because of the order of your JASON’s date string. Date() with no parameter gets the current date and time:

 

 

var d = new Date();
$.writeln(d)
//returns Fri Mar 17 2023 08:43:58 GMT-0400

 

 

If I include a parameter the string needs to be in the same order— day, month, date, year, hour, minute, sec. So:

 

 

var d = new Date("03-19-2023 00:00:00");
$.writeln(d)
//returns "Sun Mar 19 2023 00:00:00 GMT-0400"

 

 

If I reorder your object date string I get this:

 

 

data = [
    {
      "date":"03-19-2023 00:00:00",
      "start":"2023-03-19T00:00:00.000Z",
      "end":"2023-03-20T00:00:00.000Z",
      "name":"Mother's Day",
      "type":"observance",
      "rule":"easter -21"
    },
    {
      "date":"04-09-2023 00:00:00",
      "start":"2023-04-08T23:00:00.000Z",
      "end":"2023-04-09T23:00:00.000Z",
      "name":"Easter Sunday",
      "type":"observance",
      "rule":"easter"
    },
    {
      "date":"06-18-2023 00:00:00",
      "start":"2023-06-17T23:00:00.000Z",
      "end":"2023-06-18T23:00:00.000Z",
      "name":"Father's Day",
      "type":"observance",
      "rule":"3rd sunday in June"
    }
  ];

  for (var i = 0; i < data.length; i++) {
	var dx = new Date(data[i].date),
	dm = dx.getMonth(),
	dd = dx.getDate(),
	dn = data[i].name,
	dt = data[i].type;
	//alert("date: " + dx);
    $.writeln("date: " + dx + "    Month: " + (dm +1))
    /* returns:
    date: Sun Mar 19 2023 00:00:00 GMT-0400    Month: 3
    date: Sun Apr 09 2023 00:00:00 GMT-0400    Month: 4
    date: Sun Jun 18 2023 00:00:00 GMT-0400    Month: 6 */

}

 

 

1 reply

Community Expert
March 17, 2023

Maybe add a console log after the 'for' line

    console.log("date string: " + data[i].date);

 

Community Expert
March 17, 2023

Or maybe this

for (var i = 0; i < data.length; i++) {
    var dx = moment(data[i].date, "YYYY-MM-DD"),
    dm = dx.month(),
    dd = dx.date(),
    dn = data[i].name,
    dt = data[i].type;
    alert("date: " + dx);
}
PrintHouse
Inspiring
March 17, 2023

Thanks. What is "moment()"? Is it an internal function or some external lib?

I'm using

var dm = parseInt(data[i].date.substr(5, 2), 10);
var dd = parseInt(data[i].date.substr(8, 2), 10);

for now and it works fine. Still would like to know why Date behaves like the above.