TimeSpan
A timeout or elapsed interval is a duration, not a calendar date or an opening time. TimeSpan.parse() turns its string into components and aggregate values you can compare. Use totalHours when the whole interval matters, then toString() or JSON serialization when you need the duration string again.
Parsing
Section titled “Parsing”Use TimeSpan.parse() to parse C#-compatible values in the format [-][d.]hh:mm:ss[.fffffff].
import { TimeSpan } from '@cratis/fundamentals';
const span = TimeSpan.parse('1.02:30:15.25');
console.log(span.days); // 1console.log(span.hours); // 2console.log(span.minutes); // 30console.log(span.seconds); // 15Parsing requires hours, minutes, and seconds; days and the fractional part are optional. Hours must be between 0 and 23, and minutes and seconds between 0 and 59. Express a full day as 1.00:00:00, not 24:00:00. Invalid formats and out-of-range values throw an Error. A leading minus sign represents a negative interval, and fractions accept one to seven digits.
Formatting
Section titled “Formatting”Use toString() to produce a C#-compatible TimeSpan string. It pads the clock components, omits zero days, and removes trailing zeros from the fractional part. Create initialized values with parse() before formatting them.
import { TimeSpan } from '@cratis/fundamentals';
const span = TimeSpan.parse('00:05:30');console.log(span.toString()); // "00:05:30"JSON Serialization
Section titled “JSON Serialization”TimeSpan.toJSON() returns the same duration string as toString(). Native JSON.stringify() calls it automatically, so this request body contains a string rather than the component fields:
import { TimeSpan } from '@cratis/fundamentals';
const span = TimeSpan.parse('00:00:01.5');const json = JSON.stringify({ duration: span });console.log(json); // {"duration":"00:00:01.5"}Plain JSON.parse(json) restores the JSON object but leaves duration as a string. Use TimeSpan.parse(JSON.parse(json).duration) when you need a duration instance again. For fields in a typed response model, see JsonSerializer rather than assuming JSON parsing restores classes.
Available Values
Section titled “Available Values”A parsed TimeSpan provides component and aggregate values, including:
ticksdays,hours,minutes,secondsmilliseconds,microseconds,nanosecondstotalDays,totalHours,totalMinutes,totalSecondstotalMilliseconds,totalMicroseconds,totalNanoseconds
Use aggregate values for duration comparisons: totalHours includes whole days, while hours is only the remaining hour component. In the parsing example, span.hours is 2 but span.totalHours is greater than 24. For a negative duration, ticks and the aggregate values carry the sign; the hour, minute, and second components remain magnitudes.
These public fields are writable independently. Assigning a new hours value does not recalculate ticks or totalHours, and toString() formats from ticks. To represent a changed duration consistently, parse the new duration string rather than treating component assignment as arithmetic.
The public ticks and total values use JavaScript numbers and can round very large intervals. A parsed TimeSpan keeps its original tick precision when formatting or serializing, including .NET’s minimum and maximum values.
See also
Section titled “See also”- TimeOnly — represent a time of day
- DateOnly — represent a calendar date
- JsonSerializer — work with typed JSON payloads