Search code examples
node.jsdatecompareisodate

Node.js Expand compressed ISO Date Time


Is there a simple/native way to convert a short hand ISO format to an expanded standard format? I believe that is what is causing my comparison to fail (but happy to be told otherwise).

What i believe is short hand is this

Short ISO Time: 20160524T054843Z

What i have as a normal ISO string is

Long ISO Time: 2016-05-18T11:46:21.982Z

Then when i try and compare using

${compareable.getHours()}:${compareable.getMinutes()}:${compareable.getSeconds()}:${compareable.getMilliseconds()}

I get the following

Time difference: NaN:NaN:NaN:NaN

Solution

  • You could try this:

    var shortDate = '20160524T054843Z'
    shortDate.replace(/^(\d{4})(\d{2})(\d{2})T(\d{2})(\d{2})(\d{2})Z$/, '$1-$2-$3T$4:$5:$6.000Z')
    > "2016-05-24T05:48:43.000Z"