¿Hay alguna manera de obtener la hora actual en nanosegundos usando JavaScript?


Así que sé que puedo obtener la hora actual en milisegundos usando JavaScript. Pero, ¿es posible obtener la hora actual en nanosegundos en su lugar?

Author: Lightness Races in Orbit, 2011-05-14

6 answers

Lograr una precisión de microsegundos en la mayoría de los navegadores utilizando:

window.performance.now()

Véase también:

 57
Author: Jeffrey Yasskin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-03-25 23:25:23

Basándose en la respuesta de Jeffery, para obtener una marca de tiempo absoluta (como el OP quería) el código sería:

var TS = window.performance.timing.navigationStart + window.performance.now();

El resultado está en unidades de milisegundos, pero es un valor de punto flotante según se informa " con una precisión de una milésima de milisegundo".

 10
Author: Ronenz,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-12-24 22:48:34

En entornos del lado del servidor como Node.js puede usar la siguiente función para obtener tiempo en nanosegundos

function getNanoSecTime() {
  var hrTime = process.hrtime();
  return hrTime[0] * 1000000000 + hrTime[1];
}

También obtener micro segundos de una manera similar también:

function getMicSecTime() {
  var hrTime = process.hrtime();
  return hrTime[0] * 1000000 + parseInt(hrTime[1] / 1000);
}
 6
Author: Tahsin Turkoz,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-11-28 19:17:49

No. No hay ninguna posibilidad de que obtenga una precisión de nanosegundos en la capa de JavaScript.

Si está tratando de comparar alguna operación muy rápida, póngala en un bucle que la ejecute varias miles de veces.

 4
Author: Lightness Races in Orbit,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-03-24 11:09:53

JavaScript registra el tiempo en milisegundos, por lo que no podrá obtener el tiempo con esa precisión. La respuesta smart-aleck es "multiplicar por 1.000.000".

 3
Author: Ryan Lynch,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-03-24 11:12:47

¡Sí! Prueba el excelente sazze nano-tiempo

let now = require('nano-time');
now(); // '1476742925219947761' (returns as string due to JS limitation)
 1
Author: 1111161171159459134,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-08-19 16:37:41