UNPKG

@aotearoan/neon

Version:

Neon is a lightweight design library of Vue 3 components with minimal dependencies.

157 lines (156 loc) 6.67 kB
class i { /** * Format a Date object as a locale formatted date with a three letter month and a 2 digit day. * @param value The date to format. */ static formatDate(t) { const { dayFormatted: e, monthShortName: n } = i.stringToNeonDate(t.toISOString()); return `${n} ${e}`; } /** * Format an ISO 8601 string to a locale formatted date with a three letter month and a 2 digit day and the time. * @param value ISO date string * @param seconds boolean indicating whether to display seconds */ static formatISOStringToDateAndTime(t, e = !1) { const { dayFormatted: n, monthShortName: a, year: r, time: o, timeShort: g } = i.stringToNeonDate(t); return e ? `${n} ${a} ${r}, ${o}` : `${n} ${a} ${r}, ${g}`; } /** * Convert an ISO 8601 formatted string to a locale formatted date with a three letter month, a 2 digit day and a locale formatted time. * * @param date The date/time as an <a href="https://en.wikipedia.org/wiki/ISO_8601">ISO 8601</a> string. * @param locale The locale for which to convert the date for display purposes, defaults to browser locale. * @param strict Do not add time component to localise the date. * * @returns Object representing the provided date. */ static stringToNeonDate(t, e, n = !1) { const a = e || navigator.language, r = /* @__PURE__ */ new Date(), o = Intl.DateTimeFormat().resolvedOptions().timeZone, g = !(t.match(/(.*)Z/) || t.match(/(.*)[+-]\d{2}:\d{2}/)), s = /* @__PURE__ */ new Date( !n && t.length === 10 ? `${t}T${r.toISOString().split("T")[1]}` : t + (g ? "Z" : "") ); let m; t.length > 10 && (m = s.toLocaleString( "en-GB", // use en-GB for 0-23 hr offset t.length <= 16 ? { hour12: !1, hour: "2-digit", minute: "2-digit", timeZone: o } : { hour12: !1, hour: "2-digit", minute: "2-digit", second: "2-digit", timeZone: o } )); const c = { year: +s.toLocaleString("en-US", { year: "numeric", timeZone: o }), yearFormatted: s.toLocaleString(a, { year: "numeric", timeZone: o }), month: +s.toLocaleString("en-US", { month: "numeric", timeZone: o }), monthShortName: s.toLocaleString(a, { month: "short", timeZone: o }), monthLongName: s.toLocaleString(a, { month: "long", timeZone: o }), day: +s.toLocaleString("en-US", { day: "numeric", timeZone: o }), dayFormatted: s.toLocaleString(a, { day: "2-digit", timeZone: o }) }; return m && (c.time = m, c.timeShort = s.toLocaleString("en-GB", { hour12: !1, hourCycle: "h23", hour: "2-digit", minute: "2-digit", timeZone: o })), c; } /** * Convert a date object to an ISO 8601 formatted string. By default, the time is not considered. * * @param date Javascript date object. * @param time Whether to consider the time as part of the conversion to the ISO string. * * @returns an ISO 8601 formatted date, optionally with the time as well. */ static dateToIso(t, e = !1) { const n = t.toISOString(); return e ? n : n.split("T")[0]; } /** * Convert the params received into an ISO 8601 formatted string with only date in it. * * @param day The number of the day to be used. * @param month The number of the month to be used. * @param year The number of the year to be used. * * @returns an ISO 8601 formatted date for the given parameters. */ static dmyToIso(t, e, n) { return `${n}-${e < 10 ? "0" + e : e}-${t < 10 ? "0" + t : t}`; } /** * Returns the days of the week starting at Monday day name translated using the locale received as a first element. * * @param locale The locale for which to convert the date for display purposes, defaults to browser locale. * @param format The format of the returned values * * @returns an array with the days of the week named according to the locale received. */ static dowNames(t, e = "short") { const n = t || navigator.language, a = /* @__PURE__ */ new Date(), r = []; for (; !r[a.getDay()]; ) r[a.getDay()] = a.toLocaleString(n, { weekday: e }), a.setDate(a.getDate() + 1); const o = r.shift(); return o && r.push(o), r; } /** * Returns the months of the year starting at January month name translated using the locale received as a first element. * * @param locale The locale for which to convert the date for display purposes, defaults to browser locale. * @param format The format of the month names to return * * @returns an array with the month names of the year, all named according to the locale received. */ static monthNames(t, e = "short") { const n = t || navigator.language; return Array.from(Array(12).keys()).map((a) => new Date(2023, a, 15).toLocaleString(n, { month: e })); } /** * Collate & return the data required to drive the calendar popup in * <a href="/user-input/date-picker">NeonDatePicker</a>. * * @param selectedDate ISO-8601 date-only string for the selected date. * @param currentPageMonth month of the current calendar page. * @param currentPageYear year of the current calendar page. * @param currentPageDecadeStart decade start year of the current calendar page. * @param locale user's locale. * * @returns The calendar configuration. */ static toCalendarConfiguration(t, e, n, a, r) { const o = r || navigator.language, g = i.stringToNeonDate(i.dateToIso(/* @__PURE__ */ new Date()), o), s = t ? i.stringToNeonDate(t, o, !0) : void 0, m = e || g.month, c = e && n ? new Date(n, e - 1, 15) : null, L = (c == null ? void 0 : c.toLocaleString(o, { month: "long" })) || g.monthLongName, S = n || g.year, T = a || Math.floor(S / 10) * 10, d = new Date(S, (m + 11) % 12, 1), f = new Date(S, (m + 11) % 12, 1); f.setDate(f.getDate() - 1); const u = []; let D = 1, w = !1; for (let l = 0; l < 6; l++) { u[l] = []; for (let h = 0; h < 7; h++) if (l === 0 && h < f.getDay()) u[l][h] = null; else if (u[l][h] = d.getDate(), D = d.getDate(), d.setDate(d.getDate() + 1), d.getDate() === 1) { for (let y = h + 1; y < 7; y++) u[l][y] = null; w = !0; break; } if (w) break; } return { today: g, selected: s, pageMonth: m, pageMonthName: L, pageYear: S, pageDecadeStart: T, dowNames: i.dowNames(o), dowLongNames: i.dowNames(o, "long"), dates: u, lastDayOfMonth: D, months: i.monthNames(o), monthLongNames: i.monthNames(o, "long") }; } } export { i as NeonDateUtils }; //# sourceMappingURL=NeonDateUtils.es.js.map