Recursive AI

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.

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

Terminal
dotnet add package RecursiveAI.OneEcma

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
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 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.

C#
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.

C#
using RecursiveAI.OneEcma;
Console.WriteLine(new ScriptEngine().Evaluate("""
new Intl.RelativeTimeFormat('en').format(-5, 'minute');
""").AsString);
Text
5 minutes ago

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, 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', 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 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.

All oneecma recipes