---
title: "Format JavaScript durations in C#"
url: https://recursiveai.org/oneecma/cookbook/format-javascript-durations/
description: "Use JavaScript Intl.DurationFormat in C# to display the same elapsed time in English, French, or digital notation. Includes a runnable .NET example."
version: "1.1.1"
---

# Format JavaScript durations in C#

**OneEcma** is a free, open-source JavaScript interpreter for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneEcma) · [GitHub](https://github.com/recursive-ai-oss/oneecma-csharp) · [Docs](https://recursiveai.org/oneecma/docs/) · [Download example (.zip)](https://recursiveai.org/oneecma/cookbook/format-javascript-durations/format-javascript-durations.zip)

Show elapsed time as “1 hour, 5 minutes.”

```shell
dotnet add package RecursiveAI.OneEcma
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
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](https://recursiveai.org/oneecma/cookbook/format-javascript-durations/format-javascript-durations.zip), then `dotnet run`.

Prints:

```text
1 hour, 5 minutes
```

## The full program

```csharp
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:

```text
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`:

```csharp
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);
```

```text
0:00:09.37
```

**Read the display parts without splitting on punctuation.** Each part has a `type` and a `value`:

```csharp
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);
```

```text
integer: 1
literal: :
integer: 05
literal: :
integer: 00
```

## Related recipes

- [Format numbers with JavaScript Intl](https://recursiveai.org/oneecma/cookbook/format-numbers-with-javascript-intl/)
- [Control the JavaScript clock and time zone](https://recursiveai.org/oneecma/cookbook/control-javascript-clock-and-time-zone/)

## The short answer

Use [`Intl.DurationFormat`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DurationFormat/DurationFormat), 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:

```javascript
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);
```

```text
1:05:09
```

A 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](https://recursiveai.org/oneecma/docs/intl/#durationformat) truncates to the requested precision.

## What’s the difference between Intl.DurationFormat and Intl.DateTimeFormat?

[`Intl.DateTimeFormat`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/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`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/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](https://recursiveai.org/oneecma/docs/intl/) 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:

```javascript
Intl.DurationFormat.supportedLocalesOf(['ja', 'fr']);
```

```text
["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`.
