1

I have two Instant objects, and need to get one Duration object. How would I do this in JavaScript?

const start = Temporal.now.instant();
await doLongOperation();
const finish = Temporal.now.instant();

const duration = ... ?
Justin Grant
  • 44,807
  • 15
  • 124
  • 208
Vasyl Boroviak
  • 5,959
  • 5
  • 51
  • 70
  • Unrelated to the question, but I wouldn't recommend using Temporal.Now.instant() to time a long-running operation. Now.instant() basically gives you the system clock time, which can be changed by the user or by an NTP update while the operation is ongoing, and browsers may choose to lower the resolution of the clock for security reasons. – ptomato Nov 22 '22 at 19:37

1 Answers1

1

You can use either .until() or .since() methods of a Temporal.Instant object.

Until:

const duration = start.until(finish);

Since:

const duration = finish.since(start);
Vasyl Boroviak
  • 5,959
  • 5
  • 51
  • 70