0
    const time = '2022-01-13T18:40:44.748903Z';
    const date = new Date(time);
    const millisecondsSinceEpoch = date.getTime();
    const seconds = Math.floor(millisecondsSinceEpoch / 1000);

how to get nano seconds in Javascript?

icelemon
  • 827
  • 2
  • 11
  • 23
  • 1
    What are you trying to do? There is a defined formula for number of nanoseconds per millisecond. The `Date` object only has a resolution of tens of milliseconds. – Heretic Monkey Jan 13 '22 at 20:11

1 Answers1

3

Multiply the millisecondsSinceEpoch variable by 1000000 for the nanosecond conversion, this is because a nanosecond is 1000000 times smaller than a millisecond.

var nanosecondsSinceEpoch = millisecondsSinceEpoch * 1000000;