eloi
v1.0.4
Published
A lightweight JavaScript library for shifting the global Date object forward or backward in time during runtime
Downloads
334
Maintainers
Readme
eloi
A lightweight JavaScript library for shifting the global Date
object forward or backward in time during runtime. Perfect for testing systems against future or past dates without manually manipulating your system clock.
Table of Contents
🇺🇦 Help Ukraine
I am based in Kyiv, Ukraine, and during these challenging times, it is more important than ever to support Ukraine in its fight for freedom and sovereignty. The war has had a profound impact on millions of lives, and every contribution can make a difference.
We fight for democratic values, freedom, for our future! Once again Ukrainians have to stand against evil, terror, against genocide. The outcome of this war will determine what path human history is taking from now on.
💛💙 If you find this library useful, consider helping Ukrainians by donating to reputable organizations providing aid or supporting the efforts of the Ukrainian people in any way you can. Together, we can make a meaningful impact.
Motivation
Eloi was created to provide a convenient and reliable way to test how applications behave with different dates and times without requiring changes to the system clock.
This can be particularly useful for simulating future expiration dates, verifying date-based logic, or ensuring long-running systems handle transitions correctly.
By offering a runtime solution, Eloi eliminates the risks and complexities associated with altering system-wide settings, making it an invaluable tool for developers working on time-sensitive features.
Requirements
To use library you need to have node and npm installed in your machine:
- node
>=10
- npm
>=6
Package is continuously tested on darwin, linux and win32 platforms. All active and maintenance LTS node releases are supported.
Installation
To install the library run the following command
npm i --save eloi
Usage
Basic Example:
import eloi from 'eloi';
// Shift time 1 year into the future
const year = 365 * 24 * 60 * 60 * 1000;
eloi.shift(year); // 1 year in milliseconds
console.log(new Date()); // Prints future date
console.log(Date.now());
eloi.reset(); // Reset to original Date
console.log(new Date()); // Prints the current date
Advanced Example:
import { Eloi } from 'eloi';
// Create a local Eloi instance
const eloi = new Eloi(Date, { global: false });
const CustomDate = eloi.shift(-24 * 60 * 60 * 1000); // 1 day back in time
console.log(new CustomDate()); // Prints Yesterday
console.log(new Date()); // Prints Today
// Reset not required since global Date is untouched
Configuration
constructor(OriginalDate = Date, options = {})
OriginalDate
(optional): The Date implementation to use as the base.options.global
(optional): Whether to replace the global Date. Default is true.
shift(offsetMs)
Shifts time by the specified offset (in milliseconds). Returns a modified Date class.
offsetMs
: The number of milliseconds to shift. Positive for future, negative for past.
reset()
Resets the global Date object to its original implementation.
Contribute
Make the changes to the code and tests. Then commit to your branch. Be sure to follow the commit message conventions. Read Contributing Guidelines for details.