UNPKG

datetimez

Version:

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

190 lines (189 loc) 7.56 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 Math.round(this.getTime() / 1000); } set unix(_) { throw new Error('Unable to update date directly'); } setLocale(code) { __classPrivateFieldSet(this, _DateTimez_locale, code, "f"); return this; } addDate(num) { this.setDate(this.getDate() + num); return this; } 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; } 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; } 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; } subtractDate(num) { this.setDate(this.getDate() - num); return this; } 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; } 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); } isBefore(d) { return this.unix < new DateTimez(d).unix; } isAfter(d) { return this.unix > new DateTimez(d).unix; } isEqual(d) { return this.unix === new DateTimez(d).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.default = date; module.exports.DateTimez = DateTimez;