UNPKG

datetimez

Version:

a small size date (and time) formater library based on native javascript Date.

244 lines (242 loc) 9.37 kB
"use strict"; var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) { if (kind === "m") throw new TypeError("Private method is not writable"); if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it"); return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value; }; var _DateTimez_locale; class DateTimez extends Date { constructor(year, month, date, hour, minute, second) { super(!year && !month && !date && !hour && !minute && !second ? new Date() : typeof year === 'string' || typeof year === 'object' ? year : new Date(year || 1900, month || 0, date || 1, hour || 0, minute || 0, second || 0)); _DateTimez_locale.set(this, 'en'); return this; } get year() { return this.getFullYear(); } set year(_) { throw new Error('Cannot modify the property year directly. Use setFullYear instead.'); } get month() { return this.getMonth(); } set month(_) { throw new Error('Cannot modify the property month directly. Use setMonth instead.'); } get date() { return this.getDate(); } set date(_) { throw new Error('Cannot modify the property date directly. Use setDate instead.'); } get hour() { return this.getHours(); } set hour(_) { throw new Error('Cannot modify the property hour directly. Use setHours instead.'); } get minute() { return this.getMinutes(); } set minute(_) { throw new Error('Cannot modify the property minute directly. Use setMinutes instead.'); } get second() { return this.getSeconds(); } set second(_) { throw new Error('Cannot modify the property second directly. Use setSeconds instead.'); } get millisecond() { return this.getMilliseconds(); } set millisecond(_) { throw new Error('Cannot modify the property millisecond directly. Use setMilliseconds instead.'); } get monthString() { return this.toLocaleString(__classPrivateFieldGet(this, _DateTimez_locale, "f"), { month: 'long' }); } set monthString(_) { throw new Error('Property monthString is depend on month. In case you want to modify month, use setMonth instead.'); } get dayString() { return this.toLocaleString(__classPrivateFieldGet(this, _DateTimez_locale, "f"), { weekday: 'long' }); } set dayString(_) { throw new Error('Property dayString is depend on date. In case you want to modify day, use setDate instead.'); } get lastDateOfMonth() { const temp = new DateTimez(this); temp.setMonth(this.getMonth() + 1); temp.setDate(0); return temp.date; } set lastDateOfMonth(_) { throw new Error('Cannot modify lastDateOfMonth since it depend on month.'); } get locale() { return __classPrivateFieldGet(this, _DateTimez_locale, "f"); } set locale(code) { __classPrivateFieldSet(this, _DateTimez_locale, code, "f"); } get unix() { return Number(String(+this).slice(0, -3)); } set unix(_) { throw new Error('Unable to update date directly'); } /** * Set locale value using id of a string with a BCP 47 language tag, or an array of such strings. * * For more info about locale: * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#locale_identification_and_negotiation */ setLocale(code) { __classPrivateFieldSet(this, _DateTimez_locale, code, "f"); return this; } /** * Modify `DateTimez` by adding date(s) according to `num`. */ addDate(num) { this.setDate(this.getDate() + num); return this; } /** * Modify `DateTimez` by adding month(s) according to `num`. */ addMonth(num) { const target = new DateTimez(this.year, this.month + num); if (this.date > target.lastDateOfMonth) { this.setDate(target.lastDateOfMonth); } this.setMonth(this.getMonth() + num); return this; } /** * Modify `DateTimez` by adding year(s) according to `num`. */ addYear(num) { const target = new DateTimez(this.year + 1, this.month); if (this.date > target.lastDateOfMonth) { this.setDate(target.lastDateOfMonth); } this.setFullYear(this.getFullYear() + num); return this; } /** * Modify `DateTimez` by subtracting year(s) according to `num`. */ subtractYear(num) { const target = new DateTimez(this.year - 1, this.month); if (this.date > target.lastDateOfMonth) { this.setDate(target.lastDateOfMonth); } this.setFullYear(this.getFullYear() - num); return this; } /** * Modify `DateTimez` by subtracting date(s) according to `num`. */ subtractDate(num) { this.setDate(this.getDate() - num); return this; } /** * Modify `DateTimez` by subtracting month(s) according to `num`. */ subtractMonth(num) { const target = new DateTimez(this.year, this.month - num); if (this.date > target.lastDateOfMonth) { this.setDate(target.lastDateOfMonth); } this.setMonth(this.getMonth() - num); return this; } /** * Takes a string of tokens and replaces them with their corresponding values. You can freely decide the separators. */ format(format, locale = __classPrivateFieldGet(this, _DateTimez_locale, "f")) { const yearFull = this.toLocaleString(locale, { year: 'numeric' }); const year = yearFull.slice(2); const monthFull = this.toLocaleString(locale, { month: 'long' }); const monthShort = monthFull.slice(0, 3); const month2Digit = this.toLocaleString(locale, { month: '2-digit' }); const date = this.toLocaleString(locale, { day: '2-digit' }); const dayFull = this.toLocaleString(locale, { weekday: 'long' }); const dayShort = dayFull.slice(0, 3); const hour = this.toLocaleString(locale, { hour: '2-digit', hour12: false }); let minute = this.toLocaleString(locale, { minute: '2-digit' }); if (minute.length < 2) minute = `0${this.toLocaleString(locale, { minute: '2-digit' })}`; let second = this.toLocaleString(locale, { second: '2-digit' }); if (second.length < 2) second = `0${this.toLocaleString(locale, { second: '2-digit' })}`; return format .replace('YYYY', yearFull) .replace('YY', year) .replace('MMMM', monthFull) .replace('MMM', monthShort) .replace('MM', month2Digit) .replace('DD', date) .replace('dddd', dayFull) .replace('ddd', dayShort) .replace('hh', hour) .replace('mm', minute) .replace('ss', second); } /** * Check if a `DateTimez` is before another `Date`. Return `false` if equal. * * Units of measurements: Year, Month, Date, Hour, Minute, Second */ isBefore(d) { return this.unix < new DateTimez(d).unix; } /** * Check if a `DateTimez` is after another `Date`. Return `false` if equal. * * Units of measurements: Year, Month, Date, Hour, Minute, Second */ isAfter(d) { return this.unix > new DateTimez(d).unix; } /** * Check if whether a `DateTimez` is equal to another `Date`. * * Units of measurements: Year, Month, Date, Hour, Minute, Second */ isEqual(d) { return this.unix === new DateTimez(d).unix; } /** * Check if a DateTimez is between 2 other Dates. * Return true if equal to first parameter or second parameter. * * Units of measurements: Year, Month, Date, Hour, Minute, Second */ isBetween(d1, d2) { const start = new DateTimez(d1); const end = new DateTimez(d2); return (this.unix >= start.unix) && (this.unix <= end.unix); } } _DateTimez_locale = new WeakMap(); const date = (year, month, date, hour, minute, second) => new DateTimez(!year && !month && !date && !hour && !minute && !second ? new Date() : typeof year === 'string' || typeof year === 'object' ? year : new Date(year || 1990, month || 0, date || 1, hour || 0, minute || 0, second || 0)); module.exports.DateTimez = DateTimez; module.exports.default = date;