Experimental Date
object overwrite for always using UTC dates by default.
Tested on:
- Windows = Node.js 14.x and 16.x with ESM
- Linux = Node.js 14.x with ESM
Guaranteed to NOT work on Node.js 11.x and lower, this won't be fixed.
This package is ESM only.
On MacOS and Linux, if you wanted to achieve this, all you need to do is set process.env.TZ
to Etc/GMT
, however Windows does not respect this, forcing you to either change your entire system's timezone with a package like set-tz
, which personally I found vastly inconvenient when trying to selfhost apps on my personal computer, or overwritting the entire Date
object to force it to output only UTC values, which is what this package does for you.
*This package probably does work on MacOS (untested) and Linux, but it is only truly needed on Windows.
Feel free to improve it and make a PR. Bug reports are always welcome too.
npm i utc-date --save
On your main file:
import 'utc-date';
This must be placed as early as possible, prior to even other imports to ensure full utc-date usage even within other imports.
All Date
instances from here on will now be using utc-date. This includes sub-files and modules. Does NOT include Date
instances created before utc-date
is loaded.
If for some reason while using utc-date you require using the native Date object, you can import a clone of it from utc-date with:
import { NativeDate } from 'utc-date'
Keep in mind that unlike the Date
overwrite, this is not global and NativeDate
will only be accessible on the file it's imported on.
utc-date supports a few customization settings through the use of ENV variables:
Setting | Description |
---|---|
UTCDATE_NO_OVERWRITE |
When set to any value, does not automatically overwrite the Date object with the UTCDate one. (Defaults to overwriting if not set) |
UTCDATE_PATCH_CONSOLE |
When set to any value, will patch the console methods to make the logged UTC dates match what you expect from logging the native Date object. (Defaults to not patching when not set) |
UTCDATE_PATCH_INSPECT |
When set to any value, will patch the util.inspect method to make the returned values for dates from utc-date match the return values of native dates. (Defaults to not patching when not set) |
Remember to keep in mind load order of
import
s when using these options, making sure the ENV variables are loaded intoprocess.env
before utc-date is imported.
Due to the nature of this package, it is naturally incompatible with any other package or local code that also overwrites/modifies the native Date
object. Extending the Date
object is fine as long as it's done after utc-date is loaded.