Date parse issue

I have dates in the following formats (coming from kindle notes):


When I try to parse that, I get an error:

let date = "2020-07-31T22:50:55.000Z";
let dateParse = Date.parse(date).toString('MMMM d, yyyy');

Script Error: TypeError: null is not an object (evaluating 'Date.parse(date).toString')

Because date parsing works fine with other scripts, I’m thinking there’s something wrong with the format? I dunno. Shortcuts can parse this date ok, I’m not sure what’s up here.

Don’t you need to make a Date object first? This works for me:

let ds1 = "2020-07-31T22:50:55.000Z";
let ds2 = new Date(ds1).toString('MMMM d, yyyy');
1 Like

Ok, but I thought the datejs library embedded in drafts didn’t need this. The syntax I have above works in other scripts …

Glad to know the official way to do this though.

Does the date/time format you have used work in those scripts?

If I build up the string slowly I get the following results

// FYI, I'm UK based when analysing the results
let date1 = "2020-07-31";
//Evaluates to `Fri Jul 31 2020 00:00:00 GMT+0100 (BST)`

let date2 = "2020-07-31T22:50:55";
//Evaluates to `Fri Jul 31 2020 22:50:55 GMT+0100 (BST)`

let date3 = "2020-07-31T22:50:55.000";
//Evaluates to `null`
let dtAlt3 = new Date(date3);
//Evaluates to `Fri Jul 31 2020 23:50:55 GMT+0100 (BST)`

let date4 = "2020-07-31T22:50:55.000Z";
//Evaluates to `null`
let dtAlt4 = new Date(date4);
//Evaluates to `Fri Jul 31 2020 23:50:55 GMT+0100 (BST)`

let date5 = "2020-07-31T22:50:55Z";
//Evaluates to `Fri Jul 31 2020 22:50:55 GMT+0100 (BST)`

Note that while Date.parse() returns NaN, there’s a known issue whereby the overriding datejs version returns null instead.

If I try and parse the date in something without datejs, that date/time format seems to work okay - example JSfiddle.

That suggests the underlying issue is with the datejs implementation of parse(). But unless the format of the date/time is different, that doesn’t explain why it has worked for you in other scripts when doing the pate parsing this way.

No, this is what I was suspecting. That the datejs parse command, which works fine in most of the other date data I have thrown at it (mostly natural language stuff like “yesterday” or simple forms like 2020-05-01), may be choking on this particular format for some reason. Was wondering if others had run into that. It’s a shame the datejs program seems mothballed now because it is pretty good at the stuff I have thrown at it so far.

There are to things that make this error:

  1. the input from kindle seems not to be the ISO standard the last three digits for the ms are to much for the DateJs
  2. the second line assumes that the parse is working. But if it fails it is not!

Date.parse will return with null and null has no toString Method

Try to do it step by step like:

let dateParsed =  Date.parse(dateIn) //.slice(0,positionDot))
if (dateParsed === Date) dateParsed = dateParsed.toString('MMMM d, yyyy');

Maybe this makes it clear why it fails on this special occasion only.

The suggested new Operator assures that a Date object will be created and the following toString is available on this object. Nevertheless the Milliseconds have to go that dateJs can succeed.

Hope this helps

Thanks for the explanation. Very clear. I wonder why kindle produces such a weird timestamp. I can’t imagine those milliseconds are useful.

For disambiguation purposes?