Show elapsed time as “1 hour, 5 minutes.”
dotnet add package RecursiveAI.OneEcmaRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using RecursiveAI.OneEcma; var engine = new ScriptEngine(); Console.WriteLine(engine.Evaluate(""" const span = { hours: 1, minutes: 5 }; new Intl.DurationFormat('en-US', { style: 'long' }).format(span); """).AsString);
Unzip the example, then dotnet run.
Prints:
1 hour, 5 minutesThe full program
using RecursiveAI.OneEcma; var engine = new ScriptEngine(); Console.WriteLine(engine.Evaluate(""" const span = { hours: 1, minutes: 5, seconds: 9 }; const formats = [ ['en-US', 'long'], ['fr', 'long'], ['en-US', 'short'], ['en-US', 'digital'] ]; formats.map(([locale, style]) => { const text = new Intl.DurationFormat(locale, { style }).format(span); return `${locale} (${style}): ${text}`; }).join('\n'); """).AsString);
dotnet run prints:
en-US (long): 1 hour, 5 minutes, 9 seconds fr (long): 1 heure, 5 minutes et 9 secondes en-US (short): 1 hr, 5 min, 9 sec en-US (digital): 1:05:09
OneEcma ships data for en-US, en-GB, es (and es-US), fr, de, and pt-BR. Invalid fields, such as fractional hours or mixed positive and negative units, throw a JavaScript RangeError.
Variations
Show hundredths of a second. Supply whole milliseconds and set fractionalDigits to 2:
using RecursiveAI.OneEcma; var engine = new ScriptEngine(); Console.WriteLine(engine.Evaluate(""" new Intl.DurationFormat('en-US', { style: 'digital', fractionalDigits: 2 }).format({ seconds: 9, milliseconds: 375 }); """).AsString);
0:00:09.37Read the display parts without splitting on punctuation. Each part has a type and a value:
using RecursiveAI.OneEcma; var engine = new ScriptEngine(); Console.WriteLine(engine.Evaluate(""" const options = { style: 'digital' }; new Intl.DurationFormat('en-US', options) .formatToParts({ hours: 1, minutes: 5 }) .map(part => `${part.type}: ${part.value}`) .join('\n'); """).AsString);
integer: 1 literal: : integer: 05 literal: : integer: 00
Related recipes
The short answer
Use Intl.DurationFormat inside ScriptEngine.Evaluate. Pass a JavaScript object with plural unit names, such as { hours: 1, minutes: 5 }, to format, then read the resulting text with AsString. Set the locale explicitly if the output should match the language of your UI or report.
How do you format a duration as hours, minutes, and seconds in JavaScript?
Use style: 'digital' when you want a timer display. The full program passes one hour, five minutes, and nine seconds and gets 1:05:09; the two smaller fields are padded without the caller adding zeros. Pick long for full labels such as hours, short for abbreviations, or narrow for the most compact unit labels. These are formatting options, not different ways of storing a duration.
If your app stores elapsed time as a nonnegative whole number of seconds, split it into hours, minutes, and seconds before formatting:
const totalSeconds = 3909; const duration = { hours: Math.floor(totalSeconds / 3600), minutes: Math.floor((totalSeconds % 3600) / 60), seconds: totalSeconds % 60 }; new Intl.DurationFormat('en-US', { style: 'digital' }).format(duration);
1:05:09A duration object does not need all ten possible fields, but it does need at least one. Fields must be integral and use one sign. For decimal seconds, use an integer subsecond field as in the variation: its 375 milliseconds display as .37, not .38, because OneEcma’s duration formatting truncates to the requested precision.
What’s the difference between Intl.DurationFormat and Intl.DateTimeFormat?
Intl.DateTimeFormat answers “when?”: it formats an instant as a date or time on the calendar. Intl.DurationFormat answers “how long?”: the input is a set of amounts, not an instant. If you start with two timestamps, compute the elapsed time first. Passing a date or the timestamp itself as a duration doesn’t produce a difference between dates.
Use Intl.RelativeTimeFormat when the message is relative to a reference point, such as “in 5 minutes” or “5 minutes ago.” It takes a signed value and a unit, not a record with several units.
Which locales does Intl.DurationFormat support in OneEcma?
The Intl data comes from embedded CLDR tables, not the machine’s locale database. A locale outside the supported set can fall back to the host’s default locale rather than producing an error. Use Intl.DurationFormat.supportedLocalesOf to check which of your requested locales the engine can match without that default fallback:
Intl.DurationFormat.supportedLocalesOf(['ja', 'fr']);["fr"]For styled UI, use formatToParts instead of splitting the result on spaces or commas: the French output above includes nonbreaking spaces and joins the last item with et.