Manipulate current time in your application for development and staging environments
npm install fake-current-timeManipulate current time in your application for development and staging environments.
Uses Proxy and AsyncLocalStorage to isolate time contexts per request, allowing multiple users to test with different time offsets simultaneously.
``bash`
npm install fake-current-time
This example shows integration with React Router framework mode. The key concept is:
- Server: Parse offset from cookie and wrap request handling with runner.run(offset, ...)setup()
- Client: Call to initialize time manipulationsetOffset()
- UI: Use and clearOffset() to control time via cookie
`typescript
import express from "express";
import { createRequestHandler } from "@react-router/express";
import { setup, parseOffsetFromCookie } from "fake-current-time/node";
const app = express();
// Do NOT setup in production
const runner = process.env.STAGE !== "production" ? setup() : null;
app.all(
"*",
(req, _, next) => {
const offset = runner
? parseOffsetFromCookie(req.headers.cookie || "")
: undefined;
return offset && runner ? runner.run(offset, next) : next();
},
createRequestHandler({
// ...
}),
);
`
`typescript
import { startTransition } from "react";
import { setup } from "fake-current-time/browser";
// Do NOT setup in production
if (process.env.STAGE !== "production") {
setup();
}
startTransition(() => {
// ...
});
`
`typescript
import { FakeTimeController } from "fake-current-time/react";
// Restrict access to this page in production (e.g., via routing or middleware)
export function TimeControlPage() {
return
}
`
If you need to access the real Date while time manipulation is active, use OriginalDate:
`typescript
import { OriginalDate } from "fake-current-time";
const realNow = OriginalDate.now();
const realDate = new OriginalDate();
``