JavaScript locale time zone Date module
npm i locale-timezone-date
const LocaleTimezoneDate = require("locale-timezone-date");
LocaleTimezoneDate
prototype methods from default return values that are locale to the timezone of where the method has been invoked. This means that the UTC offset has been taken into account. In a certain range of the year the UTC offset changes with 1 hour, these methods provided in LocaleTimezoneDate
take such hour shifting into account. There are examples below
options
<Object> optional
ms
<Boolean> Default: true
If true
the returned value follows the notation YYYY-MM-DDThh:mm:ss.ms+UTCOffset and if false
the value will follow the notation YYYY-MM-DDThh:mm:ss+UTCOffset.
Returns: <string>
The returned value from this methodtoLocaleISOString
is a date ISO string similair to the returned value from Date.ToISOString. The returned value from this method toLocaleISOString
can be parsed by JavaScript's native Date class. The string follows the notation YYYY-MM-DDThh:mm:ss[.ms]+UTCOffset.
options
<Object> optional
ms
<Boolean> Default: true
If true
the returned value follows the notation YYYY-MM-DDThh:mm:ss.msZ and if false
the value will follow the notation YYYY-MM-DDThh:mm:ssZ.
Returns: <string>
The returned value from this methodtoFalsyLocaleISOString
is a date ISO string similair to the returned value from Date.ToISOString. However, it returnes an incorrect date ISO string because the string ends with a "Z" instead of a "+UTCOffset". A "Z" indicates the timezone offset is set to UTC0 but the string is locale and therefore it returns an incorrect value. The string follows the notation YYYY-MM-DDThh:mm:ss[.ms]Z.
Returns: <string>
The returned value from this methodyyyymmdd
is a string following the notation YYYY-MM-DD.
Returns: <string>
The returned value from this methodyyyymm
is a string following the notation YYYY-MM.
options
<Object> optional
options
<Object> optional
options
<Object> optional
const LocaleTimezoneDate = require("locale-timezone-date");
//
//
let localeDate = new LocaleTimezoneDate();
console.log(localeDate instanceof Date);
// true
//
//
/////////////////////////////////////////////////
// toLocaleISOString
//
console.log(localeDate.toLocaleISOString());
// "2021-04-09T09:15:43.062+0200"
console.log(new Date(localeDate.toLocaleISOString()).toISOString() === localeDate.toISOString());
// true
//
//
/////////////////////////////////////////////////
// toFalsyLocaleISOString
//
console.log(localeDate.toFalsyLocaleISOString());
// "2021-04-09T09:15:43.062Z"
console.log(new Date(localeDate.toFalsyLocaleISOString()).toISOString() === localeDate.toISOString());
// false
//
//
/////////////////////////////////////////////////
// yyyymmdd - TIME UTC0 - HOUR 0
//
const H00_Z = "2020-08-06T00:00:00.000Z";
localeDate = new LocaleTimezoneDate(H00_Z);
console.log(localeDate.yyyymmdd());
// "2020-08-06"
console.log(localeDate.getUTCOffset());
// { hhmm: '+0200', hour: 2 }
//
// yyyymmdd - TIME UTC0 - HOUR 22
//
const H22_Z = "2020-08-06T22:00:00.000Z";
localeDate = new LocaleTimezoneDate(H22_Z);
console.log(localeDate.yyyymmdd());
// "2020-08-07"
console.log(localeDate.getUTCOffset());
// { hhmm: '+0200', hour: 2 }
//
//
/////////////////////////////////////////////////
// yyyymmdd - LOCALE TIME - HOUR 0
//
const H00_UTC2 = "2020-08-06T00:00:00.000+0200";
localeDate = new LocaleTimezoneDate(H00_UTC2);
console.log(localeDate.yyyymmdd());
// "2020-08-06"
console.log(localeDate.getUTCOffset());
// { hhmm: '+0200', hour: 2 }
//
// yyyymmdd - LOCALE TIME - HOUR 22
//
const H22_UTC2 = "2020-08-06T22:00:00.000+0200";
localeDate = new LocaleTimezoneDate(H22_UTC2);
console.log(localeDate.yyyymmdd());
// "2020-08-06"
console.log(localeDate.getUTCOffset());
// { hhmm: '+0200', hour: 2 }
//
//
/////////////////////////////////////////////////
// msStartOfYear - START OF THE YEAR - LOCALE
//
localeDate = new LocaleTimezoneDate();
const startOfYearLocale = localeDate.startOfYear({ ms: false });
console.log(startOfYearLocale.toISOString());
// "2020-12-31T23:00:00.000Z"
console.log(startOfYearLocale.toLocaleISOString());
// "2021-01-01T00:00:00.000+0100"
//
// msStartOfYear - START OF THE YEAR - NOT LOCALE
//
const startOfYear = localeDate.startOfYear({ ms: false, locale: false });
console.log(startOfYear.toISOString());
// "2021-01-01T00:00:00.000Z"
console.log(startOfYear.toLocaleISOString());
// "2021-01-01T01:00:00.000+0100"
//
//
/////////////////////////////////////////////////
// msStartOfMonth - START OF THE MONTH - LOCALE
//
const startOfMonthLocale = localeDate.startOfMonth({ ms: false });
console.log(startOfMonthLocale.toISOString());
// "2021-03-31T22:00:00.000Z"
console.log(startOfMonthLocale.toLocaleISOString());
// "2021-04-01T00:00:00.000+0200"
//
// msStartOfMonth - START OF THE MONTH - NOT LOCALE
//
const startOfMonth = localeDate.startOfMonth({ ms: false, locale: false });
console.log(startOfMonth.toISOString());
// "2021-04-01T00:00:00.000Z"
console.log(startOfMonth.toLocaleISOString());
// "2021-04-01T02:00:00.000+0200"
//
//
/////////////////////////////////////////////////
// msStartOfDate - START OF THE DAY - LOCALE
//
const startOfDateLocale = localeDate.startOfDate({ ms: false });
console.log(startOfDateLocale.toISOString());
// "2021-04-08T22:00:00.000Z"
console.log(startOfDateLocale.toLocaleISOString());
// "2021-04-09T00:00:00.000+0200"
//
// msStartOfDate - START OF THE DAY - NOT LOCALE
//
const startOfDate = localeDate.startOfDate({ ms: false, locale: false });
console.log(startOfDate.toISOString());
// "2021-04-09T00:00:00.000Z"
console.log(startOfDate.toLocaleISOString());
// "2021-04-09T02:00:00.000+0200"