A Javascript library to mock the browser's timezone using ECMAScript Internationalization API.
npm install mock-browser-timezoneA Javascript library to mock the browser's timezone using ECMAScript Internationalization API.
Many modern browsers use the Intl API to get the desired IANA timezone string which depicts the current browser timezone.
This library is specifically designed to mock the browser timezone, assisting to test the relevant code which uses the Intl API.
##### Using npm:
```
npm install mock-browser-timezone --save-dev
##### Using yarn:
``
yarn add mock-browser-timezone -D$3
For a better implementation of mock-browser-timezone, have a look at mock-browser-timezone.js file.
`javascript
const {
registerTimezone,
registerAbbreviatedTimezone,
reset,
} = require('mock-browser-timezone');
// Mocking the timezone using IANA timezone string.
registerTimezone('Europe/Paris');
reset(); // Default back to the orginal value.
new Intl.DateTimeFormat().resolvedOptions().timeZone; // Europe/Paris
registerAbbreviatedTimezone('CET');
new Intl.DateTimeFormat([], {
timeZoneName: 'short',
})
.formatToParts(date)
.find((locale) => locale.type === 'timeZoneName').value; // CET
reset(); // Default back to the orginal abbreviated timezone value.
``
A list of supported IANA formatted timezones as well as abbreviated timezones can be found at timezones.json.