UNPKG

sussy-util

Version:
294 lines (293 loc) 9.61 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); class DateUtil { constructor() { } /** * Returns a new Date object with the current date and time. * @returns {Date} A new Date object with the current date and time. */ getCurrentDate() { return new Date(Date.now()); } /** * Returns a new Date object with the time set to midnight. * @returns {Date} A Date object with the current date and time. */ today() { const wash = this.getCurrentDate(); wash.setHours(0, 0, 0, 0); return wash; } /** * Returns a new Date object that is set to tomorrow's date. * @returns {Date} A date object that is the current date plus one day. */ tomorrow() { const today = this.today(); today.setDate(today.getDate() + 1); return today; } /** * Returns a new Date object that is set to yesterday's date. * @returns {Date} A date object that is yesterday's date. */ yesterday() { const today = this.today(); today.setDate(today.getDate() - 1); return today; } /** * Compares two dates and returns the difference between them in milliseconds. * @param {T | number | Date} dt1 - The first date. * @param {T | number | Date} dt2 - The second date. * @returns {number} The difference between the two dates in milliseconds. */ compareDates(dt1, dt2) { dt1 = this.toDate(dt1); dt2 = this.toDate(dt2); return dt1.valueOf() - dt2.valueOf(); } /** * Checks if two dates are equal. * @param {T | number | Date} dt1 - The first date. * @param {T | number | Date} dt2 - The second date. * @returns {boolean} True if the two dates are equal, false otherwise. */ equals(dt1, dt2) { dt1 = this.toDate(dt1); dt2 = this.toDate(dt2); return dt1.valueOf() === dt2.valueOf(); } /** * Returns the month abbreviation for the given month number (1-12). * @param {number} number - The month number (1-12). * @returns {string} The month abbreviation. */ getMonthAbbr(number) { return DateUtil.monthAbrs[number] ? DateUtil.monthAbrs[number] : ''; } /** * Returns the full name of the month for the given month number (1-12). * @param {number} number - The month number (1-12). * @returns {string} The full name of the month. */ getMonthFullName(number) { return DateUtil.mFullNames[number] ? DateUtil.mFullNames[number] : ''; } /** * Returns the day abbreviation for the given day number (0-6, where 0 is Sunday). * @param {number} number - The day number (0-6). * @returns {string} The day abbreviation. */ getDayAbbr(number) { return DateUtil.dayAbrs[number] ? DateUtil.dayAbrs[number] : ''; } /** * Returns the full name of the day for the given day number (0-6, where 0 is Sunday). * @param {number} number - The day number (0-6). * @returns {string} The full name of the day. */ getDayFullName(number) { return DateUtil.dFullNames[number] ? DateUtil.dFullNames[number] : ''; } /** * Checks if a given year is a leap year. * @param {number} year - The year. * @returns {boolean} True if the year is a leap year, false otherwise. */ isLeapYear(year) { return !(year % (year % 100 ? 4 : 400)); } /** * It returns the first day of the week (Monday) based on the current date. * @returns The first day of the week. */ weekFirstDay() { const nowDate = this.getCurrentDate(); return new Date(nowDate.getDate() - (nowDate.getDay() - 1) * 86400000); } /** * It returns the last day of the week. * @returns The last day of the week. */ weekLastDay() { const firstDay = this.weekFirstDay(); return new Date((firstDay.getDate() / 1000 + 6 * 86400) * 1000); } /** * Returns an array of leap years between a start and end year (inclusive). * @param {number} startYear - The start year. * @param {number} endYear - The end year. * @returns {number[]} An array of leap years. */ leapYearsInRange(startYear, endYear) { const sus = []; for (let i = startYear; i <= endYear; i++) { if (this.isLeapYear(i)) sus.push(i); } return sus; } /** * Returns the first day of the current month. * @returns {Date} A Date object representing the first day of the current month. */ getMonthFirstDay() { const nowDate = this.getCurrentDate(); return new Date(nowDate.getFullYear(), nowDate.getMonth()); } /** * Returns the last day of the current month. * @returns {Date} A Date object representing the last day of the current month. */ getMonthLastDay() { const nowDate = this.getCurrentDate(); return new Date(new Date(nowDate.getFullYear(), nowDate.getMonth() + 1).valueOf() - 86400000); } /** * Converts the input to a Date object. * @param {T | number | Date} input - The input value. * @returns {Date} A Date object. */ toDate(input) { if (typeof input === 'number') { return new Date(input); } return input; } /** * If date1 is before date2, return true, else return false. * @param {T | number | Date} date1 - T | number | Date * @param {T | number | Date} date2 - T | number | Date * @returns A boolean value. */ isAfter(date1, date2) { date1 = this.toDate(date1); date2 = this.toDate(date2); return date1.getTime() < date2.getTime(); } /** * If date1 is before date2, return true, else return false. * @param {number | Date} date1 - T | number | Date * @param {number | Date} date2 - T | number | Date * @returns A boolean value. */ isBefore(date1, date2) { date1 = this.toDate(date1); date2 = this.toDate(date2); return date1.getTime() > date2.getTime(); } /** * This function takes a number of years and returns the number of months in that number of years. * @param {number} years - number * @returns The number of months in the given number of years. */ yearsToMonths(years) { return Math.floor(years * 12); } /** * Checks if a given date falls on a weekend (Saturday or Sunday). * @param {T | number | Date} dt - The date to check. * @returns {boolean} True if the date falls on a weekend, false otherwise. */ isWeekend(dt) { const date = this.toDate(dt); const day = date.getDay(); return day === 0 || day === 6; } /** * Calculates the difference in days between two dates. * @param {T | number | Date} dt1 - The first date. * @param {T | number | Date} dt2 - The second date. * @returns {number} The difference in days between the two dates. */ getDaysDiff(dt1, dt2) { const date1 = this.toDate(dt1); const date2 = this.toDate(dt2); const diffTime = date2.getTime() - date1.getTime(); return Math.ceil(diffTime / (1000 * 3600 * 24)); } /** * Adds the specified number of days to a given date. * @param {T | number | Date} dt - The date to add days to. * @param {number} days - The number of days to add. * @returns {Date} The resulting date after adding the specified number of days. */ addDays(dt, days) { const date = this.toDate(dt); date.setDate(date.getDate() + days); return date; } /** * Formats a given date object into a string representation using the specified format. * @param {number | Date} dt - The date object to format. * @param {string} format - The format string. (e.g., 'YYYY-MM-DD') * @returns {string} The formatted date string. */ formatDate(dt, format) { const date = this.toDate(dt); const formatTokens = { YYYY: date.getFullYear().toString(), MM: String(date.getMonth() + 1).padStart(2, '0'), DD: String(date.getDate()).padStart(2, '0'), HH: String(date.getHours()).padStart(2, '0'), mm: String(date.getMinutes()).padStart(2, '0'), ss: String(date.getSeconds()).padStart(2, '0'), }; let formattedDate = format; for (const token in formatTokens) { formattedDate = formattedDate.replace(token, formatTokens[token]); } return formattedDate; } /** * Returns the current timestamp in milliseconds. * @returns {number} The current timestamp in milliseconds. */ getCurrentTimestamp() { return Date.now(); } static getInstance() { return this.instance; } } DateUtil.instance = new DateUtil(); DateUtil.monthAbrs = [ 'jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'okt', 'nov', 'dec', ]; DateUtil.mFullNames = [ 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December', ]; DateUtil.dayAbrs = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']; DateUtil.dFullNames = [ 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday', ]; exports.default = DateUtil.getInstance();