locale-timezone-date

5.0.1 • Public • Published

localeTimezoneDate

JavaScript locale time zone Date module

npm i locale-timezone-date
const LocaleTimezoneDate = require("locale-timezone-date");

Class: LocaleTimezoneDate

All of 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

localeTimezoneDate.toLocaleISOString([options]])

    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 method toLocaleISOString 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.

localeTimezoneDate.toFalsyLocaleISOString([options])

    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 method toFalsyLocaleISOString 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.

localeTimezoneDate.yyyymmdd()

    Returns: <string> The returned value from this method yyyymmdd is a string following the notation YYYY-MM-DD.

localeTimezoneDate.yyyymm()

    Returns: <string> The returned value from this method yyyymm is a string following the notation YYYY-MM.

localeTimezoneDate.startOfYear([options])

    options <Object> optional
      locale <Boolean> Default: true If locale is set to false the returned value is UTC0, otherwise is locale to the timezone.
      ms <Boolean> Default: true If ms is set to false the returned value is a localeTimezoneDate, otherwise in milliseconds after Unix Epoch.
    Returns: <integer> | <localeTimezoneDate> Default this method behaves with locale true and ms true.

localeTimezoneDate.startOfMonth([options])

    options <Object> optional
      locale <Boolean> Default: true If locale is set to false the returned value is UTC0, otherwise is locale to the timezone.
      ms <Boolean> Default: true If ms is set to false the returned value is a localeTimezoneDate, otherwise in milliseconds after Unix Epoch.
    Returns: <integer> | <localeTimezoneDate> Default this method behaves with locale true and ms true.

localeTimezoneDate.startOfDate([options])

    options <Object> optional
      locale <Boolean> Default: true If locale is set to false the returned value is UTC0, otherwise is locale to the timezone.
      ms <Boolean> Default: true If ms is set to false the returned value is a localeTimezoneDate, otherwise in milliseconds after Unix Epoch.
    Returns: <integer> | <localeTimezoneDate> Default this method behaves with locale true and ms true.

Examples

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"

Readme

Keywords

none

Package Sidebar

Install

npm i locale-timezone-date

Weekly Downloads

3

Version

5.0.1

License

MIT

Unpacked Size

16.4 kB

Total Files

3

Last publish

Collaborators

  • burndkemper