---
title: "Show yesterday and tomorrow in JavaScript"
url: https://recursiveai.org/oneecma/cookbook/format-relative-time-with-javascript-intl/
description: "Use JavaScript Intl.RelativeTimeFormat in .NET to show yesterday, today and tomorrow. Compare numeric: 'auto' with numeric labels and switch locales."
version: "1.1.1"
---

# Show yesterday and tomorrow in JavaScript

**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-relative-time-with-javascript-intl/format-relative-time-with-javascript-intl.zip)

Run JavaScript from C#. Set `numeric: 'auto'` and pass `-1`, `0` or `1` with the unit `'day'`.

```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 result = new ScriptEngine().Evaluate("""
const fmt = new Intl.RelativeTimeFormat('en',
  { numeric: 'auto' });
[-1, 0, 1].map(n => fmt.format(n, 'day'))
  .join('\n');
""");
Console.WriteLine(result.AsString);
```

```text
yesterday
today
tomorrow
```

[The example](https://recursiveai.org/oneecma/cookbook/format-relative-time-with-javascript-intl/format-relative-time-with-javascript-intl.zip) is the full program below: unzip it and `dotnet run`. To run only the code above, paste it into Program.cs of a new `dotnet new console` project and `dotnet run`.

## The full program

Run the same offsets with numeric labels and with words in English and French.

```csharp
using RecursiveAI.OneEcma;

var engine = new ScriptEngine();
Console.WriteLine(engine.Evaluate("""
const settings = [
  ['en', 'always'], ['en', 'auto'], ['fr', 'auto']
];
settings.map(([locale, numeric]) => {
  const fmt = new Intl.RelativeTimeFormat(locale, { numeric });
  const days = [-1, 0, 1].map(day => fmt.format(day, 'day'));
  return `${locale} ${numeric}: ${days.join(' / ')}`;
}).join('\n');
""").AsString);
```

```text
en always: 1 day ago / in 0 days / in 1 day
en auto: yesterday / today / tomorrow
fr auto: hier / aujourd’hui / demain
```

An invalid unit such as `'dayss'` raises a JavaScript `RangeError`; use `'day'` or `'days'`.

## Variations

**Use minutes for a label on a recent event.**

```csharp
using RecursiveAI.OneEcma;
Console.WriteLine(new ScriptEngine().Evaluate("""
new Intl.RelativeTimeFormat('en').format(-5, 'minute');
""").AsString);
```

```text
5 minutes ago
```

## Related recipes

- [Format JavaScript dates by locale](https://recursiveai.org/oneecma/cookbook/format-javascript-dates-by-locale/)
- [Format JavaScript durations](https://recursiveai.org/oneecma/cookbook/format-javascript-durations/)

## The short answer

Set `numeric: 'auto'` and call `format` with a signed day offset and the unit `'day'`. In English, `-1`, `0` and `1` become “yesterday,” “today” and “tomorrow.” The C# example runs this JavaScript inside a .NET process with OneEcma.

## How do you format relative time in JavaScript?

[Pass a number and a unit](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat/format), not a `Date`. Use negative offsets for the past and positive offsets for the future. The unit tells the formatter what that number means: the variation passes `-5` and `'minute'` to display “5 minutes ago.” It doesn’t measure time or decide when to change units. If you’re displaying activity timestamps, choose the offset and unit first; for a calendar label like “yesterday,” compute the difference between calendar dates in the time zone your UI uses rather than counting elapsed 24-hour periods.

## What’s the difference between numeric auto and always in Intl.RelativeTimeFormat?

The default, `numeric: 'always'`, keeps a numeric label: “1 day ago,” “in 0 days” and “in 1 day” in the sample. With [`numeric: 'auto'`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat/RelativeTimeFormat), the formatter uses a word or phrase when the locale has one for that offset and unit. It can still return a numeric label; `auto` means words when available, not words only.

Pass the UI’s locale as the first constructor argument rather than translating three English strings yourself. In the example, `fr` uses “hier,” “aujourd’hui” and “demain” for the same offsets. OneEcma uses [embedded locale data](https://recursiveai.org/oneecma/docs/intl/) for a set of locales rather than relying on data installed on the host; an unsupported request can fall back to the default locale. Treat the resulting text as a display label: make scheduling or sorting decisions from the underlying offset or timestamp, not from the translated string.
