UNPKG

@opensig/opendesign

Version:

42 lines (41 loc) 1.61 kB
"use strict"; Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const dayjs = require("dayjs"); const is = require("./is.js"); const pad = (n, maxLength = 2) => String(n).padStart(maxLength, "0"); const dateTimeNumberToString = ({ year, month, date, hour, minute, second }, { format, timeOnly = false }) => { const toValidate = timeOnly ? [hour, minute, second] : [year, month, date, hour, minute, second]; if (toValidate.some((v) => is.isNil(v))) { return void 0; } const dayjsDate = dayjs(`${is.isNil(year) ? 1970 : year}-${is.isNil(month) ? 1 : month}-${is.isNil(date) ? 1 : date} ${hour}:${minute}:${second}`); return dayjsDate.isValid() ? dayjsDate.format(format) : void 0; }; function stringToDateTimeNumber(str, { timeOnly } = {}) { if (is.isNil(str)) { return void 0; } const dayjsDate = dayjs(timeOnly && !str.includes(" ") ? `1970-1-1 ${str}` : str); const dateInfo = { year: isNaN(dayjsDate.get("year")) ? null : dayjsDate.get("year"), month: isNaN(dayjsDate.get("month")) ? null : dayjsDate.get("month") + 1, date: isNaN(dayjsDate.get("date")) ? null : dayjsDate.get("date") }; const timeInfo = { hour: isNaN(dayjsDate.get("hour")) ? null : dayjsDate.get("hour"), minute: isNaN(dayjsDate.get("minute")) ? null : dayjsDate.get("minute"), second: isNaN(dayjsDate.get("second")) ? null : dayjsDate.get("second") }; return timeOnly ? timeInfo : { ...dateInfo, ...timeInfo }; } exports.dateTimeNumberToString = dateTimeNumberToString; exports.pad = pad; exports.stringToDateTimeNumber = stringToDateTimeNumber;