spanMs
Signature
Section titled “Signature”spanMs(start: string, end: string): number | nullReturn the exact elapsed milliseconds between two ISO 8601 instant strings.
- The universal span primitive for profiling and observability. Fractional, like
performance.now()— a sub-millisecond span is reported as a fraction, not rounded away. - Signed: negative when
startis afterend, sospanMs(b, a)is exactly-spanMs(a, b). - Measures exact elapsed time, never calendar distance. A wall-clock day containing a DST transition is 23 or 25 hours (or 24.5, in
Australia/Lord_Howe), andspanMsreports the hours that actually elapsed.spanWallClockanswers the calendar question; conflating the two is the most common span bug there is. - Accepts anything
toNanosecondsdoes: the RFC 9557 instant grammar, no leap seconds. An offset designator (Z,±HH:MM[:SS[.fraction]]) is required and nothing follows it: a time zone, calendar or any other annotation returnsnull, so a zoned string is not an instant here (spanWallClockis the one that reads those). The offset alone fixes the instant, and one written with seconds is read exactly. - Returns
nullon invalid input, not0—0is the span between an instant and itself.null, notNaN, because that is GMT’s sentinel for every number-returning function: it is the one a caller already checks for, and the only onestrictNullChecksforces them to handle. ANaNwould type as plainnumberand propagate silently through arithmetic.spanNsandspanWallClockreturnnullfor the same reason. - Safe range. Returns
nullwhen the exact span exceedsNumber.MAX_SAFE_INTEGERmilliseconds (±9_007_199_254_740_991 ms, about ±285,000 years), which two instants at opposite ends ofTemporal.Instant’s range do. A sub-millisecond fraction counts toward that ceiling, so any number returned is a safe integer or below. UsespanNspast it — it carries the exact value as abigint. Sub-millisecond fidelity goes first, long before the ceiling: a double carries the nanosecond digits exactly only while the span is under 2^53 ns, roughly 104 days. - Leap seconds are not counted. UTC repeats a second rather than numbering a 61st one, so a span across one is a second short of the physical elapsed time; against a smeared clock (Google, AWS, Meta) the error is up to a second spread over the smear window. For a leap-second-exact span, convert both ends with
toTAIor addleapSecondsBetween(start, end)seconds, both fromtimescale/.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
start | string | ISO 8601 instant string the span is measured from |
end | string | ISO 8601 instant string the span is measured to |
Returns
Section titled “Returns”exact elapsed milliseconds, negative when start is after end, or null on invalid input
Examples
Section titled “Examples”import { spanMs } from "@northguild/gmt/span/calculate";
spanMs("2024-03-10T12:00:00Z", "2024-03-10T12:00:01Z") // 1000Source
Section titled “Source”Accessibility
© 2026 NorthGuild · Licences
Code examples: MIT-0. Packages: MIT. Third-party data and components: their own licences, credited where they appear. Everything else: © 2026 NorthGuild, all rights reserved.