UNPKG

@modern-kit/utils

Version:
46 lines (42 loc) 1.72 kB
'use strict'; var dateParseDate = require('../parseDate/index.cjs'); require('../parseDateString/index.cjs'); function getDateEndOf(date, unit) { const parsedDate = dateParseDate.parseDate(date); const isUTC = unit.startsWith("utc"); const baseUnit = isUTC ? unit.slice(3).toLowerCase() : unit; const year = isUTC ? parsedDate.getUTCFullYear() : parsedDate.getFullYear(); const month = isUTC ? parsedDate.getUTCMonth() : parsedDate.getMonth(); const day = isUTC ? parsedDate.getUTCDate() : parsedDate.getDate(); switch (baseUnit) { case "year": if (isUTC) { return new Date(Date.UTC(year, 11, 31, 23, 59, 59, 999)); } return new Date(year, 11, 31, 23, 59, 59, 999); case "month": { const lastDayOfMonth = isUTC ? new Date(Date.UTC(year, month + 1, 0)).getUTCDate() : new Date(year, month + 1, 0).getDate(); if (isUTC) { return new Date(Date.UTC(year, month, lastDayOfMonth, 23, 59, 59, 999)); } return new Date(year, month, lastDayOfMonth, 23, 59, 59, 999); } case "week": { const dayOfWeek = isUTC ? parsedDate.getUTCDay() : parsedDate.getDay(); const endOfWeek = day + (6 - dayOfWeek); if (isUTC) { return new Date(Date.UTC(year, month, endOfWeek, 23, 59, 59, 999)); } return new Date(year, month, endOfWeek, 23, 59, 59, 999); } case "date": if (isUTC) { return new Date(Date.UTC(year, month, day, 23, 59, 59, 999)); } return new Date(year, month, day, 23, 59, 59, 999); default: throw new Error(`\uC9C0\uC6D0\uD558\uC9C0 \uC54A\uB294 \uB2E8\uC704\uC785\uB2C8\uB2E4`); } } exports.getDateEndOf = getDateEndOf; //# sourceMappingURL=index.cjs.map