UNPKG

khmer-date-utils

Version:

This package is used to convert date to khmer date, date range, date utils

104 lines 4.64 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.GetDateRange = GetDateRange; exports.CountMonths = CountMonths; const types_1 = require("./types"); /** * @param type * @param StartDay * @returns */ function GetDateRange(type) { const today = new Date(); switch (type) { case types_1.ETypeDate.Now: return { dateType: type, startDate: today, endDate: today }; case types_1.ETypeDate.Yesterday: { const yesterday = new Date(today); yesterday.setDate(today.getDate() - 1); return { dateType: type, startDate: yesterday, endDate: yesterday }; } case types_1.ETypeDate.Tomorrow: { const tomorrow = new Date(today); tomorrow.setDate(today.getDate() + 1); return { dateType: type, startDate: tomorrow, endDate: tomorrow }; } case types_1.ETypeDate.LastWeek: { const lastWeekStart = new Date(today); lastWeekStart.setDate(today.getDate() - today.getDay() + 1); const lastWeekEnd = new Date(lastWeekStart); lastWeekEnd.setDate(lastWeekStart.getDate() + 6); return { dateType: type, startDate: lastWeekStart, endDate: lastWeekEnd }; } case types_1.ETypeDate.ThisWeek: { const thisWeekStart = new Date(today); thisWeekStart.setDate(today.getDate() - today.getDay() + (today.getDay() == 0 ? -6 : 1)); const thisWeekEnd = new Date(thisWeekStart); thisWeekEnd.setDate(thisWeekStart.getDate() + 6); if (thisWeekEnd > today) { thisWeekEnd.setDate(today.getDate()); } return { dateType: type, startDate: thisWeekStart, endDate: thisWeekEnd }; } case types_1.ETypeDate.LastMonth: { const lastMonthStart = new Date(today); lastMonthStart.setMonth(today.getMonth() - 1, 1); const lastMonthEnd = new Date(lastMonthStart); lastMonthEnd.setMonth(lastMonthStart.getMonth() + 1, 0); return { dateType: type, startDate: lastMonthStart, endDate: lastMonthEnd }; } case types_1.ETypeDate.BeforeLastYear: { const beforeLastYearStart = new Date(today); beforeLastYearStart.setFullYear(today.getFullYear() - 2, 0, 1); // Two years before const beforeLastYearEnd = new Date(beforeLastYearStart); beforeLastYearEnd.setFullYear(beforeLastYearStart.getFullYear(), 11, 31); // December 31st of that year return { dateType: type, startDate: beforeLastYearStart, endDate: beforeLastYearEnd }; } case types_1.ETypeDate.LastYear: { const lastYearStart = new Date(today); lastYearStart.setFullYear(today.getFullYear() - 1, 0, 1); const lastYearEnd = new Date(lastYearStart); lastYearEnd.setFullYear(lastYearStart.getFullYear(), 11, 31); return { dateType: type, startDate: lastYearStart, endDate: lastYearEnd }; } case types_1.ETypeDate.MonthToDate: { const monthToDateStart = new Date(today); monthToDateStart.setDate(1); return { dateType: type, startDate: monthToDateStart, endDate: today }; } case types_1.ETypeDate.YearToDate: { const yearToDateStart = new Date(today); yearToDateStart.setMonth(0, 1); return { dateType: type, startDate: yearToDateStart, endDate: today }; } default: throw new Error(`Invalid date type: ${type}`); } } /** * Counts the number of months between two dates. * @param startDate * @param endDate * @returns */ function CountMonths(startDate, endDate) { if (isNaN(startDate.getTime()) || isNaN(endDate.getTime())) { throw new Error("Invalid date objects provided."); } if (endDate < startDate) { throw new Error("The end date must be after the start date."); } const startYear = startDate.getFullYear(); const startMonth = startDate.getMonth(); const endYear = endDate.getFullYear(); const endMonth = endDate.getMonth(); let monthCount = (endYear - startYear) * 12 + (endMonth - startMonth); // Adjust for full-month inclusion if start is first day and end is last day const isStartFirstDay = startDate.getDate() === 1; const isEndLastDay = new Date(endDate.getFullYear(), endDate.getMonth() + 1, 0).getDate() === endDate.getDate(); if (isStartFirstDay && isEndLastDay) { monthCount += 1; } return monthCount; } //# sourceMappingURL=core.js.map