UNPKG

@onesy/date

Version:

Time and date utils library

115 lines (114 loc) 5.63 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.daysWeekAbr = exports.daysWeek = exports.monthsAbr = exports.months = exports.units = void 0; const merge_1 = __importDefault(require("@onesy/utils/merge")); const optionsDefault = {}; exports.units = ['millisecond', 'milliseconds', 'second', 'minute', 'minutes', 'hour', 'hours', 'day', 'days', 'dayWeek', 'dayYear', 'week', 'weeks', 'month', 'months', 'year']; exports.months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']; exports.monthsAbr = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']; exports.daysWeek = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']; exports.daysWeekAbr = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']; class OnesyDate { constructor(value_ = new Date(), options = {}) { this.value_ = value_; this.options = options; this.init(); } static get utc() { return new OnesyDate(new Date(), { utc: true }); } static get daysInMonth() { return new OnesyDate().daysInMonth; } static get valueOf() { return new OnesyDate().valueOf; } static get unix() { return new OnesyDate().unix; } static get milliseconds() { return new OnesyDate().milliseconds; } static get iso() { return new OnesyDate().iso; } static get onesyDate() { return new OnesyDate(); } static get local() { return new OnesyDate().local; } init() { // Merge options with option defaults this.options = (0, merge_1.default)(this.options, optionsDefault); // Make a date object from it this.value = new Date((this.value_.valid ? this.value_.value : this.value_)); if (this.valid) { this.millisecond = this.value[this.options.utc ? 'getUTCMilliseconds' : 'getMilliseconds'](); this.milliseconds = this.value.getTime(); this.second = this.value[this.options.utc ? 'getUTCSeconds' : 'getSeconds'](); this.minute = this.value[this.options.utc ? 'getUTCMinutes' : 'getMinutes'](); this.minutes = Math.ceil(this.milliseconds / (1e3 * 60)); this.hour = this.value[this.options.utc ? 'getUTCHours' : 'getHours'](); this.hours = Math.ceil(this.milliseconds / (1e3 * 60 * 60)); this.day = this.value[this.options.utc ? 'getUTCDate' : 'getDate'](); this.days = Math.ceil(this.milliseconds / (1e3 * 60 * 60 * 24)); this.dayWeek = this.value[this.options.utc ? 'getUTCDay' : 'getDay'](); // https://stackoverflow.com/a/64293860 ty this.weeks = Math.ceil(((this.milliseconds / 1000) + 345600) / 604800); this.month = this.value[this.options.utc ? 'getUTCMonth' : 'getMonth']() + 1; this.year = this.value[this.options.utc ? 'getUTCFullYear' : 'getFullYear'](); this.dayYear = Math.ceil((this.milliseconds - Number(new Date(this.year, 0, 0))) / 1000 / 60 / 60 / 24); this.months = ((this.year - 1970) * 12) - (12 - this.month); this.weekValue(); } } weekValue() { const WEEK_MILLISECONDS = 604800000; const firstDayOfWeek = 1; const startOfYear = new Date(this.year, 0, 1); startOfYear.setDate(startOfYear.getDate() + (firstDayOfWeek - (startOfYear.getDay() % 7))); this.week = Math.round((Number(this.value) - Number(startOfYear)) / WEEK_MILLISECONDS) + 1; return this.week; } get monthsNames() { var _a; return ((_a = this.options.overrides) === null || _a === void 0 ? void 0 : _a.months) || exports.months; } get monthsAbr() { var _a; return ((_a = this.options.overrides) === null || _a === void 0 ? void 0 : _a.monthsAbr) || exports.monthsAbr; } get daysWeek() { var _a; return ((_a = this.options.overrides) === null || _a === void 0 ? void 0 : _a.daysWeek) || exports.daysWeek; } get daysWeekAbr() { var _a; return ((_a = this.options.overrides) === null || _a === void 0 ? void 0 : _a.daysWeekAbr) || exports.daysWeekAbr; } get valid() { var _a; return (this.value_ === undefined || this.value_ instanceof Date || this.value_ instanceof OnesyDate || typeof this.value_ === 'number') && !Number.isNaN(Math.ceil(((_a = this.value) === null || _a === void 0 ? void 0 : _a.getTime()) / 1000)); } get local() { if (this.valid) return new OnesyDate(new Date(this.value.toLocaleString('en-us'))); } get utc() { return new OnesyDate(this.value, { utc: true }); } get iso() { if (this.valid) return this.value.toISOString(); } get daysInMonth() { return new Date(this.year, this.month, 0).getDate(); } get weeksInYear() { const date = new Date(this.year, 0, 1); const isLeapYear = new Date(this.year, 1, 29).getMonth() === 1; return (date.getDay() === 4 || isLeapYear && date.getDay() === 3) ? 53 : 52; } get valueOf() { return this.milliseconds; } get unix() { if (this.valid) return Math.floor(this.value.getTime() / 1000); } timezone(value) { if (this.valid && value) return new OnesyDate(this.value.toLocaleString('en-us', { timeZone: value })); } } exports.default = OnesyDate;