• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Strange parsing of Date object

Explorer ,
Mar 17, 2023 Mar 17, 2023

Copy link to clipboard

Copied

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?

TOPICS
Scripting

Views

542

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Mar 17, 2023 Mar 17, 2023

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"

...

Votes

Translate

Translate
Community Expert ,
Mar 17, 2023 Mar 17, 2023

Copy link to clipboard

Copied

Maybe add a console log after the 'for' line

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

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 17, 2023 Mar 17, 2023

Copy link to clipboard

Copied

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);
}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 17, 2023 Mar 17, 2023

Copy link to clipboard

Copied

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. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 17, 2023 Mar 17, 2023

Copy link to clipboard

Copied

Moment provides various methods to parse, manipulate, and format dates.

The output you see from the alert() function includes the date, month, year, time, and time zone information, which is the default string representation of a Date object.

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 17, 2023 Mar 17, 2023

Copy link to clipboard

Copied

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 */

}

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 17, 2023 Mar 17, 2023

Copy link to clipboard

Copied

Correct but one would hope the ISO date format would be recognised by Adobe scripting language (JavaScript has no problems in parsing it). As mentioned above, I can't change the JSON data, so will for now stick to parsing specific bits from the String.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 17, 2023 Mar 17, 2023

Copy link to clipboard

Copied

Extendscript is based on the 25-year-old JS ECMA 3 standard and hasn't been updated since. Adobe is currently rolling out its new UXP scripting language that will provide us with more modern features,  but we've been stuck in the old times for a while. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 17, 2023 Mar 17, 2023

Copy link to clipboard

Copied

LATEST

Not sure if this is any better, but you could also rearrange the JSON string to something ExtendScript recognizes as a date object:

 

 

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"
    }
  ];

  for (var i = 0; i < data.length; i++) {
    var a = data[i].date.split(" ");
    var d = a[0].split("-");
	var dx = new Date(d[1] + "-" + d[2] + "-" + d[0]+" "+ a[1]);
    
    dm = dx.getMonth()
	dd = dx.getDate()
	dn = data[i].name,
	dt = data[i].type;

    $.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 */
}

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines