adwaveui
Version:
Interactive Web Components inspired by the Gtk Adwaita theme.
147 lines (145 loc) • 3.32 kB
JavaScript
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
// src/components/calendar/date.ts
var Dt = class _Dt {
constructor(_date = /* @__PURE__ */ new Date(), locale) {
this._date = _date;
this._locale = navigator.language;
if (locale) {
this._locale = locale;
}
}
static {
__name(this, "Dt");
}
static isParsable(date) {
const ts = Date.parse(date);
if (isNaN(ts)) {
return false;
}
return true;
}
static parse(date) {
const ts = Date.parse(date);
if (!isNaN(ts)) {
return new _Dt(new Date(ts));
}
throw new Error("Invalid date format.");
}
// mutators
setLocale(l) {
this._locale = l;
return this;
}
setYear(y) {
this._date.setFullYear(y);
return this;
}
setMonth(m) {
this._date.setMonth(m - 1);
return this;
}
startOfMonth() {
this._date.setDate(1);
return this;
}
endOfMonth() {
this._date.setDate(this.countDaysOfThisMonth());
return this;
}
setHour(h, m, sec) {
this._date.setHours(h);
this._date.setMinutes(m);
this._date.setSeconds(sec);
this._date.setMilliseconds(0);
return this;
}
setDay(d) {
d = Math.max(0, d);
d = Math.min(this.countDaysOfThisMonth(), d);
this._date.setDate(d);
return this;
}
plus(amounts) {
if (amounts.days != null) {
this._date.setDate(this._date.getDate() + Math.abs(amounts.days));
}
return this;
}
minus(amounts) {
if (amounts.days != null) {
this._date.setDate(this._date.getDate() - Math.abs(amounts.days));
}
return this;
}
// getters
year() {
return this._date.getFullYear();
}
month() {
return this._date.getMonth() + 1;
}
monthString() {
return new Intl.DateTimeFormat(this._locale, {
month: "long"
}).format(this._date);
}
dayOfMonth(type) {
if (type === "padded") {
return new Intl.DateTimeFormat(this._locale, {
day: "2-digit"
}).format(this._date);
}
return new Intl.DateTimeFormat(this._locale, {
day: "numeric"
}).format(this._date);
}
dayOfWeek() {
const d = this._date.getDay();
if (d === 0) return 7;
return d;
}
dayOfWeekString(format = "long") {
return new Intl.DateTimeFormat(this._locale, {
weekday: format
}).format(this._date);
}
countDaysOfThisMonth() {
const nextMonth = new Date(
this._date.getFullYear(),
this._date.getMonth() + 1,
1
);
nextMonth.setDate(nextMonth.getDate() - 1);
return nextMonth.getDate();
}
localeString(dateStyle = "long") {
return new Intl.DateTimeFormat(this._locale, {
dateStyle
}).format(this._date);
}
iso() {
return this._date.toISOString();
}
isoDate() {
return `${this.year()}-${String(this.month()).padStart(2, "0")}-${String(this.dayOfMonth("padded"))}`;
}
jsDate() {
const d = new Date(this._date);
return d;
}
unix() {
return this._date.getTime();
}
clone() {
const d = new Date(this._date);
return new _Dt(d).setLocale(this._locale);
}
// utility
isSameDate(d) {
return this.year() == d.year() && this.month() == d.month() && this.dayOfMonth() == d.dayOfMonth();
}
};
export {
Dt
};