UNPKG

@cloudcome/utils-core

Version:
242 lines (241 loc) 7.12 kB
import { b as DATE_DAY_MS } from "./const.mjs"; import { c, d, a, e, D } from "./const.mjs"; import { d as dateParse, a as dateFormat } from "./core.mjs"; import { T, i } from "./core.mjs"; import { s as stringFormat } from "./string2.mjs"; import { isArray } from "./type.mjs"; const dateOfStartMap = [ ["s", (d2) => d2.setMilliseconds(0)], ["m", (d2) => d2.setSeconds(0)], ["h", (d2) => d2.setMinutes(0)], ["D", (d2) => d2.setHours(0)], ["W", (d2) => d2.setHours(0)], ["M", (d2) => d2.setDate(1)], ["Y", (d2) => d2.setMonth(0)] ]; function _dateStart(dateValue, symbol = "D") { const date = dateParse(dateValue); for (const [sym, fn] of dateOfStartMap) { fn(date); if (symbol === sym) break; } return date; } function dateStartInSecond(dateValue) { return _dateStart(dateValue, "s"); } function dateStartInMinute(dateValue) { return _dateStart(dateValue, "m"); } function dateStartInHour(dateValue) { return _dateStart(dateValue, "h"); } function dateStartInDay(dateValue) { return _dateStart(dateValue, "D"); } function dateStartInMonth(dateValue) { return _dateStart(dateValue, "M"); } function dateStartInYear(dateValue) { return _dateStart(dateValue, "Y"); } const dateOfEndMap = [ ["s", (d2) => d2.setMilliseconds(999)], ["m", (d2) => d2.setSeconds(59)], ["h", (d2) => d2.setMinutes(59)], ["D", (d2) => d2.setHours(23)], [ "M", (d2) => { const d22 = dateParse(d2); d22.setMonth(d2.getMonth() + 1); d22.setDate(0); d2.setDate(d22.getDate()); } ], [ "Y", (d2) => { d2.setMonth(11); d2.setDate(31); } ] ]; function _dateEnd(dateValue, symbol = "D") { const date = dateParse(dateValue); for (const [sym, fn] of dateOfEndMap) { fn(date); if (symbol === sym) break; } return date; } function dateEndInSecond(dateValue) { return _dateEnd(dateValue, "s"); } function dateEndInMinute(dateValue) { return _dateEnd(dateValue, "m"); } function dateEndInHour(dateValue) { return _dateEnd(dateValue, "h"); } function dateEndInDay(dateValue) { return _dateEnd(dateValue, "D"); } function dateEndInMonth(dateValue) { return _dateEnd(dateValue, "M"); } function dateEndInYear(dateValue) { return _dateEnd(dateValue, "Y"); } function _dateDays(dateValue, unit) { const d2 = dateParse(dateValue); const ds = unit === "M" ? dateStartInMonth(d2) : dateStartInYear(d2); const de = unit === "M" ? dateEndInMonth(d2) : dateEndInYear(d2); return Math.ceil((de.getTime() - ds.getTime()) / DATE_DAY_MS); } function dateDaysInMonth(dateValue) { return _dateDays(dateValue, "M"); } function dateDaysInYear(dateValue) { return _dateDays(dateValue, "Y"); } const defaultDiffTemplates = [ [0, 10, "刚刚"], [1, 60, "{n} 秒前", "{n} 秒后"], [60, 60 * 60, "{n} 分钟前", "{n} 分钟后"], [60 * 60, 60 * 60 * 24, "{n} 小时前", "{n} 小时后"], [0, 60 * 60 * 24 * 2, "昨天", "明天"], [0, 60 * 60 * 24 * 3, "前天", "后天"], [60 * 60 * 24, 60 * 60 * 24 * 30, "{n} 天前", "{n} 天后"], [0, Number.POSITIVE_INFINITY, "YYYY年MM月DD日"] ]; function dateRelative(dateValue, refDateValue, templates) { const now = Date.now(); const refDateValueFinal = isArray(refDateValue) ? now : refDateValue || now; const templatesFinal = isArray(templates) ? templates : isArray(refDateValue) ? refDateValue : defaultDiffTemplates; const d1 = dateParse(dateValue); const d2 = dateParse(refDateValueFinal); const diff = d1.getTime() - d2.getTime(); const isAgo = diff < 0; const absDiff = Math.abs(diff); let relative = ""; for (const [base, max, agoTemplate, featureTemplate] of templatesFinal) { const unitFinal = base * 1e3; const maxFinal = max * 1e3; if (absDiff < maxFinal) { const template = isAgo ? agoTemplate : featureTemplate || agoTemplate; const length = unitFinal === 0 ? 0 : Math.max(Math.floor(absDiff / unitFinal), 1); relative = unitFinal === 0 ? dateFormat(dateValue, template) : stringFormat(template, { n: length }); break; } } return relative; } function isLeapYear(year) { if (year % 4 !== 0) return false; if (year % 100 !== 0) return true; if (year % 400 !== 0) return false; return true; } function _isSameDateIn(date1, date2, sameSymbol = "D") { const defines = [ ["Y", (d3) => d3.getFullYear()], ["M", (d3) => d3.getMonth()], ["D", (d3) => d3.getDate()], ["h", (d3) => d3.getHours()], ["m", (d3) => d3.getMinutes()], ["s", (d3) => d3.getSeconds()], ["S", (d3) => d3.getMilliseconds()] ]; const d1 = dateParse(date1); const d2 = dateParse(date2); for (const [sym, fn] of defines) { if (fn(d1) !== fn(d2)) { return false; } if (sym === sameSymbol) break; } return true; } function isSameDateInYear(date1, date2) { return _isSameDateIn(date1, date2, "Y"); } function isSameDateInMonth(date1, date2) { return _isSameDateIn(date1, date2, "M"); } function isSameDateInDay(date1, date2) { return _isSameDateIn(date1, date2, "D"); } function isSameDateInHour(date1, date2) { return _isSameDateIn(date1, date2, "h"); } function isSameDateInMinute(date1, date2) { return _isSameDateIn(date1, date2, "m"); } function isSameDateInSecond(date1, date2) { return _isSameDateIn(date1, date2, "s"); } var EWeekStart = /* @__PURE__ */ ((EWeekStart2) => { EWeekStart2[EWeekStart2["sunday"] = 0] = "sunday"; EWeekStart2[EWeekStart2["monday"] = 1] = "monday"; EWeekStart2[EWeekStart2["tuesday"] = 2] = "tuesday"; EWeekStart2[EWeekStart2["wednesday"] = 3] = "wednesday"; EWeekStart2[EWeekStart2["thursday"] = 4] = "thursday"; EWeekStart2[EWeekStart2["friday"] = 5] = "friday"; EWeekStart2[EWeekStart2["saturday"] = 6] = "saturday"; return EWeekStart2; })(EWeekStart || {}); function _dateWeeks(dateValue, type, weekStart = 0) { const date = dateParse(dateValue); const year = date.getFullYear(); const month = date.getMonth(); const firstDate = type === "Y" ? new Date(year, 0, 1) : new Date(year, month, 1); const firstWeek = firstDate.getDay(); const days = Math.ceil((date.getTime() - firstDate.getTime()) / DATE_DAY_MS); return Math.ceil((firstWeek + days - weekStart) / 7); } function weeksOfYear(dateValue, weekStart = 0) { return _dateWeeks(dateValue, "Y", weekStart); } function weeksOfMonth(dateValue, weekStart = 0) { return _dateWeeks(dateValue, "M", weekStart); } export { DATE_DAY_MS, c as DATE_HOUR_MS, d as DATE_MINUTE_MS, a as DATE_MONTH_MS, e as DATE_SECOND_MS, D as DATE_YEAR_MS, EWeekStart, T as TzDate, _dateWeeks, dateDaysInMonth, dateDaysInYear, dateEndInDay, dateEndInHour, dateEndInMinute, dateEndInMonth, dateEndInSecond, dateEndInYear, dateFormat, dateParse, dateRelative, dateStartInDay, dateStartInHour, dateStartInMinute, dateStartInMonth, dateStartInSecond, dateStartInYear, isLeapYear, isSameDateInDay, isSameDateInHour, isSameDateInMinute, isSameDateInMonth, isSameDateInSecond, isSameDateInYear, i as isValidDate, weeksOfMonth, weeksOfYear }; //# sourceMappingURL=date.mjs.map