@onesy/date
Version:
Time and date utils library
191 lines (141 loc) • 6.31 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import merge from '@onesy/utils/merge';
const optionsDefault = {};
export const units = ['millisecond', 'milliseconds', 'second', 'minute', 'minutes', 'hour', 'hours', 'day', 'days', 'dayWeek', 'dayYear', 'week', 'weeks', 'month', 'months', 'year'];
export const months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
export const monthsAbr = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
export const daysWeek = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
export const daysWeekAbr = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'];
export default class OnesyDate {
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;
}
constructor() {
let value_ = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : new Date();
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
this.value_ = value_;
this.options = options;
_defineProperty(this, "value", void 0);
_defineProperty(this, "millisecond", void 0);
_defineProperty(this, "milliseconds", void 0);
_defineProperty(this, "second", void 0);
_defineProperty(this, "minute", void 0);
_defineProperty(this, "minutes", void 0);
_defineProperty(this, "hour", void 0);
_defineProperty(this, "hours", void 0);
_defineProperty(this, "day", void 0);
_defineProperty(this, "days", void 0);
_defineProperty(this, "dayWeek", void 0);
_defineProperty(this, "dayYear", void 0);
_defineProperty(this, "week", void 0);
_defineProperty(this, "weeks", void 0);
_defineProperty(this, "month", void 0);
_defineProperty(this, "months", void 0);
_defineProperty(this, "year", void 0);
this.init();
}
init() {
// Merge options with option defaults
this.options = merge(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 _this$options$overrid;
return ((_this$options$overrid = this.options.overrides) === null || _this$options$overrid === void 0 ? void 0 : _this$options$overrid.months) || months;
}
get monthsAbr() {
var _this$options$overrid2;
return ((_this$options$overrid2 = this.options.overrides) === null || _this$options$overrid2 === void 0 ? void 0 : _this$options$overrid2.monthsAbr) || monthsAbr;
}
get daysWeek() {
var _this$options$overrid3;
return ((_this$options$overrid3 = this.options.overrides) === null || _this$options$overrid3 === void 0 ? void 0 : _this$options$overrid3.daysWeek) || daysWeek;
}
get daysWeekAbr() {
var _this$options$overrid4;
return ((_this$options$overrid4 = this.options.overrides) === null || _this$options$overrid4 === void 0 ? void 0 : _this$options$overrid4.daysWeekAbr) || daysWeekAbr;
}
get valid() {
var _this$value;
return (this.value_ === undefined || this.value_ instanceof Date || this.value_ instanceof OnesyDate || typeof this.value_ === 'number') && !Number.isNaN(Math.ceil(((_this$value = this.value) === null || _this$value === void 0 ? void 0 : _this$value.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
}));
}
}