UNPKG

@gez/date-time-kit

Version:

44 lines (43 loc) 1.27 kB
import { getCurrentTzOffsetMs, granHelper } from "../../utils/index.mjs"; export const defaultDateFormatter = (time, { min, max }) => { const s = time.toLocaleDateString("en-GB"); const idx = { year: [6, 10], month: [3, 5], day: [0, 2] }; if (!granHelper.isDateGran(max)) max = "year"; if (!granHelper.isDateGran(min)) min = "day"; return s.slice(idx[min][0], idx[max][1]); }; export const defaultTimeFormatter = (time, { min, max }) => { const t = new Date(+time - getCurrentTzOffsetMs()).toISOString(); const idx = { hour: [11, 13], minute: [14, 16], second: [17, 19], millisecond: [20, 23] }; if (!granHelper.isTimeGran(max)) max = "hour"; if (!granHelper.isTimeGran(min)) min = "millisecond"; return t.slice(idx[max][0], idx[min][1]); }; export const defaultDatetimeFormatter = (time, { min, max }) => { const datePart = defaultDateFormatter(time, { max: granHelper.toDateGran(max), min: "day" }); const timePart = defaultTimeFormatter(time, { max: "hour", min: granHelper.toTimeGran(min) }); return "".concat(datePart, " ").concat(timePart); }; export const defaultFormatter = { date: defaultDateFormatter, time: defaultTimeFormatter, datetime: defaultDatetimeFormatter };