0

I found a format 2020-01-16 07:29:43.657519000 Z in a project and I am wondering what exactly do these numbers 657519000 mean and how do I get this exact format with js? If I try something like new Date().toISOString() I get 2020-05-27T20:25:03.369Z which is similar but not exactly the same.

Thanks for help

mitchK
  • 17
  • 4
  • It's fractions of a second. – VLAZ May 27 '20 at 20:36
  • How do I get these fractions of a second? Is there any js method to get this format? – mitchK May 27 '20 at 20:46
  • Have a look at this [answer](https://stackoverflow.com/a/15302113/7629494) – Kunal Virk May 27 '20 at 20:47
  • This is really a duplicate of [*How to format a JavaScript date*](https://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date?r=SearchResults&s=1|1450.4192). For simplicity, just use *toISOString* and mess about with some string methods to replace the T with space and add the nanoseconds. – RobG May 28 '20 at 02:49

1 Answers1

0

Browsers typically provide time values in milliseconds. The DOMHighResTimeStamp was intended to provide accuracy to microsecond and while some browsers toyed with it, I don't think any currently provide that level of accuracy.

The timestamp in the OP is to nanosecond level, that's one millionth of a millisecond. I don't think browsers or other implementations will be at that level of precision for some time and the resulting time value is beyond the range of values that ECMAScript numbers can safely represent. Any numbers after the 3rd decimal place are just noise and will be mostly nines (9s) and zeros (0s).

Anyway, in theory you can get a the current millisecond value from toISOString, then update it with values from the performance object to get whatever precision it provides, e.g.

// Current time in seconds precise to ±5 microseconds with
// nanosecond precision
function getSeconds() {
  return ((performance.timing.navigationStart + performance.now()) / 1000).toFixed(9);
}

// Get timestamp to nanosecond precision
function getTimestamp() {
  let s = getSeconds();
  // Create a Date from the time value as milliseconds
  let d = new Date(s * 1000);
  // Tack on the decimal seconds from the time value
  return d.toISOString().replace('T',' ').replace(/\d\d(\.\d+)?Z$/, (s%60).toFixed(9) + ' Z');
}

console.log(getTimestamp());

The nanosecond precision is not nanosecond accuracy if only because of the function call, which likely takes a couple of nanoseconds itself (and the previously mentioned inability of ECMAScript to represent the required number of significant digits). Plus the system clock is extremely unlikely to be that accurate, so you might as well just tack on 6 zeros to the decimal part of the seconds from toISOString:

console.log(new Date().toISOString().replace('T', ' ').replace('Z', '000000 Z'));

and not pretend to be more accurate than reasonable. That may change when systems start to support more accurate high precision time values and BigInt is available to preserve the decimal milliseconds (though there will still be the issue with accuracy).

RobG
  • 124,520
  • 28
  • 153
  • 188