dotnet add package RecursiveAI.OneEcmaRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using RecursiveAI.OneEcma; var engine = new ScriptEngine(new() { Host = new FixedHost() }); Console.WriteLine(engine.Evaluate( "Date.now() + ',' + new Date().getHours()" ).AsString); class FixedHost : DefaultHost { public override double CurrentTimeMs() => 0; public override string TimeZoneId => "UTC"; }
0,0Unzip the example, then dotnet run.
The full program
using RecursiveAI.OneEcma; var engine = new ScriptEngine(new() { Host = new BoundaryHost() }); Console.WriteLine(engine.Evaluate("Date.now()").AsNumber); Console.WriteLine(engine.Evaluate("new Date().toISOString()").AsString); Console.WriteLine(engine.Evaluate(""" ['2024-03-10T06:59:00Z', '2024-03-10T07:00:00Z'] .map(t => { const d = new Date(t); return d.getHours() + ':' + String(d.getMinutes()).padStart(2, '0') + ' offset=' + d.getTimezoneOffset(); }).join('\n') """).AsString); // Deliberately models only this spring transition, not a full zone history. class BoundaryHost : DefaultHost { private const double Transition = 1710054000000; public override double CurrentTimeMs() => Transition; public override string TimeZoneId => "America/New_York"; public override bool TryGetTimeZoneOffset( string zoneId, double epochMs, out int offsetMs, out bool isDaylight) { isDaylight = epochMs >= Transition; offsetMs = (isDaylight ? -4 : -5) * 60 * 60 * 1000; return zoneId == TimeZoneId; } }
dotnet run prints:
1710054000000 2024-03-10T07:00:00.000Z 1:59 offset=300 3:00 offset=240
BoundaryHost fixes the clock at the spring transition and supplies offsets on each side. It models only this one transition, not New York’s complete time zone history. The local time skips from 1:59 to 3:00 while UTC advances by one minute.
Variations
Compare a date-only input, a local date-time and an explicitly UTC instant under a UTC host:
using RecursiveAI.OneEcma; var engine = new ScriptEngine(new() { Host = new FixedHost() }); Console.WriteLine(engine.Evaluate(""" JSON.stringify([ Date.parse('2024-07-04'), Date.parse('2024-07-04T00:00:00'), new Date('2024-07-04T00:00:00Z').getUTCHours() ]) """).AsString); class FixedHost : DefaultHost { public override string TimeZoneId => "UTC"; }
[1720051200000,1720051200000,0]Related recipes
The short answer
Supply a host through ScriptEngineOptions.Host. Derive from DefaultHost, override CurrentTimeMs() to freeze the clock, and override TimeZoneId to select the local zone. The snippet at the top uses the Unix epoch and UTC, so both Date.now() and the local hour are zero.
For a repeatable daylight saving boundary, also override TryGetTimeZoneOffset. The full program supplies its own offsets rather than reading time zone rules from the operating system. These are separate controls: fixing the current instant does not, by itself, fix local-time calculations for other instants.
How do I mock Date in JavaScript from C#?
Change the engine’s host clock instead of replacing the JavaScript Date constructor. The Date documentation identifies IJsHost.CurrentTimeMs() as the clock source for Date.now(), new Date() and Date() called as a function. One host setting therefore reaches all three clock-dependent paths without a script-side patch.
CurrentTimeMs() returns milliseconds since the Unix epoch. The full program uses the integer 1710054000000, displayed as 2024-03-10T07:00:00.000Z. Repeated clock reads return that same value because the override returns a constant. Constructing a date from a supplied timestamp still uses that timestamp, not the fixed current time.
This controls JavaScript running inside this engine. It does not change the computer’s clock or dates read by unrelated .NET code. Keep your test’s fixed instant in the host rather than changing global machine state.
How do I mock the JavaScript time zone?
Set TimeZoneId to the intended zone. DefaultHost uses UTC by default and provides offsets through the machine’s .NET time zone rules. That is convenient for ordinary applications, but a zone name alone is not a portable set of rules: machines can have different historical data.
For a fixture that must not depend on that data, supply the offsets too. TryGetTimeZoneOffset receives the zone ID and an instant in epoch milliseconds and returns an offset in milliseconds plus a daylight-saving flag. The full program returns minus five hours before the chosen transition and minus four afterward.
The sample’s two-rule table is deliberately not a production time zone implementation. It is sufficient for the two instants it demonstrates. For dates outside that fixture, use a complete rule source rather than extending one spring transition to the entire calendar.
How do I test a daylight saving time change?
Use explicit UTC inputs on both sides of the transition, then inspect local getters. The program’s inputs are one minute apart in UTC. Their local hours jump forward because the supplied offset changes at the boundary. JavaScript’s getTimezoneOffset() uses minutes with the opposite sign from the host’s offset: New York’s negative offsets become positive 300 and 240.
For parsing tests, distinguish date-only strings from date-time strings. The pinned docs specify that date-only input is UTC, while a date-time without an offset uses local time. The variation places both under UTC, where they produce equal timestamps. An explicit Z is preferable when an input represents a universal instant rather than a local wall-clock appointment. Freezing the clock cannot remove that parsing distinction.