It is common for browsers to provide time values in milliseconds. While the DOMHighResTimeStamp aimed to offer microsecond accuracy, current browsers do not seem to achieve that level of precision.
The timestamp mentioned is at a nanosecond level, which is incredibly precise but likely beyond what browsers or other systems can accurately represent. Any numbers beyond the third decimal place may be insignificant noise filled with nines (9s) and zeros (0s).
In theory, one could retrieve the current millisecond value using toISOString, and then incorporate additional values from the performance object to enhance precision:
// Example code for getting time to nanosecond precision
function getSeconds() {
return ((performance.timing.navigationStart + performance.now()) / 1000).toFixed(9);
}
function getTimestamp() {
let s = getSeconds();
let d = new Date(s * 1000);
return d.toISOString().replace('T',' ').replace(/\d\d(\.\d+)?Z$/, (s%60).toFixed(9) + ' Z');
}
console.log(getTimestamp());
Although achieving nanosecond precision may be challenging due to function call delays and ECMAScript limitations, you can still add six zeros to the seconds from toISOString for reasonable accuracy:
console.log(new Date().toISOString().replace('T', ' ').replace('Z', '000000 Z'));
As advancements in technology lead to increased support for accurate high precision time values and the adoption of BigInt, we may see improvements in preserving decimal milliseconds. However, challenges with achieving true accuracy will remain.