@org-quicko/core 1.1.0 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/browser/utils/date/DateUtil.js +6 -1
- package/dist/browser/utils/date/DateUtil.js.map +1 -1
- package/dist/cjs/utils/date/DateUtil.cjs +5 -0
- package/dist/cjs/utils/date/DateUtil.cjs.map +1 -1
- package/dist/esm/utils/date/DateUtil.js +6 -1
- package/dist/esm/utils/date/DateUtil.js.map +1 -1
- package/dist/types/src/utils/date/DateUtil.d.ts +8 -0
- package/package.json +1 -1
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { TZDate } from '@date-fns/tz';
|
|
2
|
-
import { parse, isValid, format, startOfDay, endOfDay, differenceInCalendarDays, differenceInCalendarMonths, differenceInCalendarYears, compareAsc, add, isLeapYear } from 'date-fns';
|
|
2
|
+
import { parse, isValid, format, startOfDay, endOfDay, differenceInCalendarDays, differenceInCalendarMonths, differenceInYears, differenceInCalendarYears, compareAsc, add, isLeapYear } from 'date-fns';
|
|
3
3
|
import { IllegalArgumentException } from '../../exceptions/IllegalArgumentException.js';
|
|
4
4
|
|
|
5
5
|
/**
|
|
@@ -76,6 +76,11 @@ class BaseDateUtil {
|
|
|
76
76
|
const second = typeof secondDate === 'number' ? new Date(secondDate) : secondDate;
|
|
77
77
|
return Math.abs(differenceInCalendarMonths(first, second));
|
|
78
78
|
}
|
|
79
|
+
static anniversaryYearsInBetween(firstDate, secondDate) {
|
|
80
|
+
const first = typeof firstDate === 'number' ? new Date(firstDate) : firstDate;
|
|
81
|
+
const second = typeof secondDate === 'number' ? new Date(secondDate) : secondDate;
|
|
82
|
+
return Math.abs(differenceInYears(first, second));
|
|
83
|
+
}
|
|
79
84
|
static yearsInBetween(firstDate, secondDate) {
|
|
80
85
|
const first = typeof firstDate === 'number' ? new Date(firstDate) : firstDate;
|
|
81
86
|
const second = typeof secondDate === 'number' ? new Date(secondDate) : secondDate;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DateUtil.js","sources":["../../../../src/utils/date/DateUtil.ts"],"sourcesContent":["import { TZDate } from '@date-fns/tz';\r\nimport { add, compareAsc, differenceInCalendarDays, differenceInCalendarMonths, differenceInCalendarYears, Duration, endOfDay, format, isLeapYear, isValid, parse, startOfDay } from 'date-fns';\r\nimport { IllegalArgumentException } from '../../exceptions';\r\n\r\n/**\r\n * Utility class for date and time operations. Provides methods for parsing,\r\n * formatting, and performing calculations with dates and times.\r\n */\r\nclass BaseDateUtil {\r\n\r\n static readonly ISO_8601_FORMAT = \"yyyy-MM-dd'T'HH:mm:ss.SSSxxx\"\r\n\r\n /**\r\n * Gets the current date and time in the specified time zone.\r\n * @param timeZone The IANA time zone identifier.\r\n * @returns Current date and time in the specified time zone.\r\n */\r\n protected static now(timeZone: string): Date {\r\n return new TZDate(new Date(), timeZone);\r\n }\r\n\r\n /**\r\n * Gets the current time in milliseconds since the Unix epoch.\r\n * @returns Current time in milliseconds.\r\n */\r\n static nowInMillis(): number {\r\n return Date.now();\r\n }\r\n\r\n /**\r\n * Parses a date string into a Date object.\r\n * @param dateString The date string to parse.\r\n * @param dateFormat The format of the date string (default: ISO_8601_FORMAT).\r\n * @param timeZone The IANA time zone identifier.\r\n * @throws {IllegalArgumentException} If the date string is invalid.\r\n * @returns Parsed Date object.\r\n */\r\n protected static readDate(dateString: string, dateFormat = this.ISO_8601_FORMAT, timeZone: string): Date {\r\n const date = parse(dateString, dateFormat, TZDate.tz(timeZone));\r\n if (!isValid(date)) {\r\n throw new IllegalArgumentException('Invalid date string or date format');\r\n }\r\n return date;\r\n }\r\n\r\n /**\r\n * Formats a Date or timestamp into a string.\r\n * @param date The date or timestamp to format.\r\n * @param timeZone The IANA time zone identifier.\r\n * @param dateFormat The desired output format (default: ISO_8601_FORMAT).\r\n * @returns Formatted date string.\r\n */\r\n protected static printDate(date: Date, timeZone: string, dateFormat?: string): string;\r\n protected static printDate(date: number, timeZone: string, dateFormat?: string): string;\r\n protected static printDate(date: Date | number, timeZone: string, dateFormat: string = this.ISO_8601_FORMAT): string {\r\n const normalizedDate = typeof date === 'number' ? new Date(date) : date;\r\n return format(new TZDate(normalizedDate, timeZone), dateFormat);\r\n }\r\n\r\n /**\r\n * Gets the start of the day for a given date or timestamp.\r\n * @param date The date or timestamp to calculate from.\r\n * @param timeZone The IANA time zone identifier.\r\n * @returns The start of the day as a Date object.\r\n */\r\n protected static getStartOfDay(date: Date, timeZone: string): Date;\r\n protected static getStartOfDay(date: number, timeZone: string): Date;\r\n protected static getStartOfDay(date: Date | number, timeZone: string): Date {\r\n const normalizedDate = typeof date === 'number' ? new Date(date) : date;\r\n return startOfDay(new TZDate(normalizedDate, timeZone));\r\n }\r\n\r\n /**\r\n * Gets the end of the day for a given date or timestamp.\r\n * @param date The date or timestamp to calculate from.\r\n * @param timeZone The IANA time zone identifier.\r\n * @returns The end of the day as a Date object.\r\n */\r\n protected static getEndOfDay(date: Date, timeZone: string): Date;\r\n protected static getEndOfDay(date: number, timeZone: string): Date;\r\n protected static getEndOfDay(date: Date | number, timeZone: string): Date {\r\n const normalizedDate = typeof date === 'number' ? new Date(date) : date;\r\n return endOfDay(new TZDate(normalizedDate, timeZone));\r\n }\r\n\r\n /**\r\n * Checks if a date string is valid according to the specified format.\r\n * @param dateString The date string to validate.\r\n * @param dateFormat Optional date format (default: ISO_8601_FORMAT).\r\n * @returns True if the date is valid, false otherwise.\r\n */\r\n static isValidDate(dateString: string, dateFormat?: string): boolean {\r\n try {\r\n DateUtil.readDate(dateString, dateFormat);\r\n return true;\r\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\r\n } catch (error) {\r\n return false;\r\n }\r\n }\r\n\r\n /**\r\n * Calculates the absolute number of days between two dates or timestamps.\r\n * @param firstDate The first date or timestamp.\r\n * @param secondDate The second date or timestamp.\r\n * @returns Number of days between the two dates.\r\n */\r\n static daysInBetween(firstDate: Date, secondDate: Date): number;\r\n static daysInBetween(firstDate: number, secondDate: number): number;\r\n static daysInBetween(firstDate: Date | number, secondDate: Date | number): number {\r\n const first = typeof firstDate === 'number' ? new Date(firstDate) : firstDate;\r\n const second = typeof secondDate === 'number' ? new Date(secondDate) : secondDate;\r\n return Math.abs(differenceInCalendarDays(first, second));\r\n }\r\n\r\n /**\r\n * Calculates the absolute number of months between two dates or timestamps.\r\n * @param firstDate The first date or timestamp.\r\n * @param secondDate The second date or timestamp.\r\n * @returns The number of months between the two dates.\r\n */\r\n static monthsInBetween(firstDate: Date, secondDate: Date): number;\r\n static monthsInBetween(firstDate: number, secondDate: number): number;\r\n static monthsInBetween(firstDate: Date | number, secondDate: Date | number): number {\r\n const first = typeof firstDate === 'number' ? new Date(firstDate) : firstDate;\r\n const second = typeof secondDate === 'number' ? new Date(secondDate) : secondDate;\r\n return Math.abs(differenceInCalendarMonths(first, second));\r\n }\r\n\r\n /**\r\n * Calculates the absolute number of years between two dates or timestamps.\r\n * @param firstDate The first date or timestamp.\r\n * @param secondDate The second date or timestamp.\r\n * @returns The number of years between the two dates.\r\n */\r\n static yearsInBetween(firstDate: Date, secondDate: Date): number;\r\n static yearsInBetween(firstDate: number, secondDate: number): number;\r\n static yearsInBetween(firstDate: Date | number, secondDate: Date | number): number {\r\n const first = typeof firstDate === 'number' ? new Date(firstDate) : firstDate;\r\n const second = typeof secondDate === 'number' ? new Date(secondDate) : secondDate;\r\n return Math.abs(differenceInCalendarYears(first, second));\r\n }\r\n\r\n /**\r\n * Compares two dates or timestamps.\r\n * @param firstDate The first date or timestamp.\r\n * @param secondDate The second date or timestamp.\r\n * @returns `-1` if the first date is earlier, `1` if it is later, or `0` if the two dates are equal.\r\n */\r\n static compareDates(firstDate: Date, secondDate: Date): -1 | 0 | 1;\r\n static compareDates(firstDate: number, secondDate: number): -1 | 0 | 1;\r\n static compareDates(firstDate: Date | number, secondDate: Date | number): -1 | 0 | 1 {\r\n const first = typeof firstDate === 'number' ? new Date(firstDate) : firstDate;\r\n const second = typeof secondDate === 'number' ? new Date(secondDate) : secondDate;\r\n\r\n return compareAsc(first, second) as -1 | 0 | 1;\r\n }\r\n\r\n /**\r\n * Adds a duration (e.g., days, months, years) to a given date or timestamp.\r\n * @param date The base date or timestamp to which the duration will be added.\r\n * @param duration An object specifying the duration (e.g., `{ days: 1, months: 2 }`).\r\n * @returns A new Date object with the duration added.\r\n */\r\n static addDuration(date: Date, duration: Duration): Date;\r\n static addDuration(date: number, duration: Duration): Date;\r\n static addDuration(date: Date | number, duration: Duration): Date {\r\n const normalizedDate = typeof date === 'number' ? new Date(date) : date;\r\n return add(normalizedDate, duration);\r\n }\r\n\r\n /**\r\n * Determines whether a given year is a leap year.\r\n * @param year The year to check.\r\n * @returns `true` if the year is a leap year, otherwise `false`.\r\n */\r\n static isLeapYear(year: number): boolean {\r\n return isLeapYear(DateUtil.readDate(year.toString(), 'yyyy'));\r\n }\r\n\r\n\r\n /**\r\n * Converts a timestamp (in milliseconds) to a Date object.\r\n * @param milliseconds The timestamp in milliseconds since the Unix epoch.\r\n * @returns A Date object representing the provided timestamp.\r\n */\r\n static fromMillis(milliseconds: number): Date {\r\n return new Date(milliseconds);\r\n }\r\n\r\n /**\r\n * Converts a Date object to a timestamp (in milliseconds).\r\n * @param date The Date object to convert.\r\n * @returns The timestamp in milliseconds since the Unix epoch.\r\n */\r\n static toMillis(date: Date): number {\r\n return date.getTime();\r\n }\r\n}\r\n\r\n/**\r\n * Utility class for handling date operations in a fixed time zone.\r\n * Extends BaseDateUtil with default settings for the 'Asia/Kolkata' time zone.\r\n */\r\nexport class DateUtil extends BaseDateUtil {\r\n /** Default time zone used for all date operations. */\r\n static readonly TIMEZONE = 'Asia/Kolkata';\r\n /**\r\n * Gets the current date and time in the default time zone.\r\n * @returns Current date and time as a Date object.\r\n */\r\n static override now(): Date {\r\n return BaseDateUtil.now(this.TIMEZONE);\r\n }\r\n\r\n /**\r\n * Parses a date string into a Date object using the default time zone.\r\n * @param dateString The date string to parse.\r\n * @param dateFormat Optional format of the date string (default: ISO_8601_FORMAT).\r\n * @throws {IllegalArgumentException} If the date string is invalid.\r\n * @returns Parsed Date object.\r\n */\r\n static override readDate(dateString: string, dateFormat?: string): Date {\r\n return BaseDateUtil.readDate(dateString, dateFormat || DateUtil.ISO_8601_FORMAT, this.TIMEZONE);\r\n }\r\n\r\n /**\r\n * Formats a Date or timestamp into a string using the default time zone.\r\n * @param date The date or timestamp to format.\r\n * @param dateFormat Optional desired output format (default: ISO_8601_FORMAT).\r\n * @returns Formatted date string.\r\n */\r\n static override printDate(date: Date, dateFormat?: string): string;\r\n static override printDate(date: number, dateFormat?: string): string;\r\n static override printDate(date: Date | number, dateFormat = DateUtil.ISO_8601_FORMAT): string {\r\n const normalizedDate = typeof date === 'number' ? new Date(date) : date;\r\n return BaseDateUtil.printDate(normalizedDate, this.TIMEZONE, dateFormat);\r\n }\r\n\r\n /**\r\n * Gets the start of the day for a given date or timestamp using the default time zone.\r\n * @param date The date or timestamp to calculate from.\r\n * @returns A Date object representing the start of the day.\r\n */\r\n static override getStartOfDay(date: Date): Date;\r\n static override getStartOfDay(date: number): Date;\r\n static override getStartOfDay(date: Date | number): Date {\r\n const normalizedDate = typeof date === 'number' ? new Date(date) : date;\r\n return BaseDateUtil.getStartOfDay(normalizedDate, this.TIMEZONE);\r\n }\r\n\r\n /**\r\n * Gets the end of the day for a given date or timestamp using the default time zone.\r\n * @param date The date or timestamp to calculate from.\r\n * @returns A Date object representing the end of the day.\r\n */\r\n static override getEndOfDay(date: Date): Date;\r\n static override getEndOfDay(date: number): Date;\r\n static override getEndOfDay(date: Date | number): Date {\r\n const normalizedDate = typeof date === 'number' ? new Date(date) : date;\r\n return BaseDateUtil.getEndOfDay(normalizedDate, this.TIMEZONE);\r\n }\r\n}\r\n"],"names":[],"mappings":";;;;AAIA;;;AAGG;AACH,MAAM,YAAY,CAAA;aAEE,IAAe,CAAA,eAAA,GAAG,8BAA8B,CAAA;AAEhE;;;;AAIG;IACO,OAAO,GAAG,CAAC,QAAgB,EA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C,UAAkB,EAAE,UAAmB,EAAA;AAC5D,QAAA,OAAO,YAAY,CAAC,QAAQ,CAAC,UAAU,EAAE,UAAU,IAAI,QAAQ,CAAC,eAAe,EAAE,IAAI,CAAC,QAAQ,CAAC;;IAWnG,OAAgB,SAAS,CAAC,IAAmB,EAAE,UAAU,GAAG,QAAQ,CAAC,eAAe,EAAA;AAChF,QAAA,MAAM,cAAc,GAAG,OAAO,IAAI,KAAK,QAAQ,GAAG,IAAI,IAAI,CAAC,IAAI,CAAC,GAAG,IAAI;AACvE,QAAA,OAAO,YAAY,CAAC,SAAS,CAAC,cAAc,EAAE,IAAI,CAAC,QAAQ,EAAE,UAAU,CAAC;;IAU5E,OAAgB,aAAa,CAAC,IAAmB,EAAA;AAC7C,QAAA,MAAM,cAAc,GAAG,OAAO,IAAI,KAAK,QAAQ,GAAG,IAAI,IAAI,CAAC,IAAI,CAAC,GAAG,IAAI;QACvE,OAAO,YAAY,CAAC,aAAa,CAAC,cAAc,EAAE,IAAI,CAAC,QAAQ,CAAC;;IAUpE,OAAgB,WAAW,CAAC,IAAmB,EAAA;AAC3C,QAAA,MAAM,cAAc,GAAG,OAAO,IAAI,KAAK,QAAQ,GAAG,IAAI,IAAI,CAAC,IAAI,CAAC,GAAG,IAAI;QACvE,OAAO,YAAY,CAAC,WAAW,CAAC,cAAc,EAAE,IAAI,CAAC,QAAQ,CAAC;;;;;;"}
|
|
1
|
+
{"version":3,"file":"DateUtil.js","sources":["../../../../src/utils/date/DateUtil.ts"],"sourcesContent":["import { TZDate } from '@date-fns/tz';\r\nimport { add, compareAsc, differenceInCalendarDays, differenceInCalendarMonths, differenceInCalendarYears, differenceInYears, Duration, endOfDay, format, isLeapYear, isValid, parse, startOfDay } from 'date-fns';\r\nimport { IllegalArgumentException } from '../../exceptions';\r\n\r\n/**\r\n * Utility class for date and time operations. Provides methods for parsing,\r\n * formatting, and performing calculations with dates and times.\r\n */\r\nclass BaseDateUtil {\r\n\r\n static readonly ISO_8601_FORMAT = \"yyyy-MM-dd'T'HH:mm:ss.SSSxxx\"\r\n\r\n /**\r\n * Gets the current date and time in the specified time zone.\r\n * @param timeZone The IANA time zone identifier.\r\n * @returns Current date and time in the specified time zone.\r\n */\r\n protected static now(timeZone: string): Date {\r\n return new TZDate(new Date(), timeZone);\r\n }\r\n\r\n /**\r\n * Gets the current time in milliseconds since the Unix epoch.\r\n * @returns Current time in milliseconds.\r\n */\r\n static nowInMillis(): number {\r\n return Date.now();\r\n }\r\n\r\n /**\r\n * Parses a date string into a Date object.\r\n * @param dateString The date string to parse.\r\n * @param dateFormat The format of the date string (default: ISO_8601_FORMAT).\r\n * @param timeZone The IANA time zone identifier.\r\n * @throws {IllegalArgumentException} If the date string is invalid.\r\n * @returns Parsed Date object.\r\n */\r\n protected static readDate(dateString: string, dateFormat = this.ISO_8601_FORMAT, timeZone: string): Date {\r\n const date = parse(dateString, dateFormat, TZDate.tz(timeZone));\r\n if (!isValid(date)) {\r\n throw new IllegalArgumentException('Invalid date string or date format');\r\n }\r\n return date;\r\n }\r\n\r\n /**\r\n * Formats a Date or timestamp into a string.\r\n * @param date The date or timestamp to format.\r\n * @param timeZone The IANA time zone identifier.\r\n * @param dateFormat The desired output format (default: ISO_8601_FORMAT).\r\n * @returns Formatted date string.\r\n */\r\n protected static printDate(date: Date, timeZone: string, dateFormat?: string): string;\r\n protected static printDate(date: number, timeZone: string, dateFormat?: string): string;\r\n protected static printDate(date: Date | number, timeZone: string, dateFormat: string = this.ISO_8601_FORMAT): string {\r\n const normalizedDate = typeof date === 'number' ? new Date(date) : date;\r\n return format(new TZDate(normalizedDate, timeZone), dateFormat);\r\n }\r\n\r\n /**\r\n * Gets the start of the day for a given date or timestamp.\r\n * @param date The date or timestamp to calculate from.\r\n * @param timeZone The IANA time zone identifier.\r\n * @returns The start of the day as a Date object.\r\n */\r\n protected static getStartOfDay(date: Date, timeZone: string): Date;\r\n protected static getStartOfDay(date: number, timeZone: string): Date;\r\n protected static getStartOfDay(date: Date | number, timeZone: string): Date {\r\n const normalizedDate = typeof date === 'number' ? new Date(date) : date;\r\n return startOfDay(new TZDate(normalizedDate, timeZone));\r\n }\r\n\r\n /**\r\n * Gets the end of the day for a given date or timestamp.\r\n * @param date The date or timestamp to calculate from.\r\n * @param timeZone The IANA time zone identifier.\r\n * @returns The end of the day as a Date object.\r\n */\r\n protected static getEndOfDay(date: Date, timeZone: string): Date;\r\n protected static getEndOfDay(date: number, timeZone: string): Date;\r\n protected static getEndOfDay(date: Date | number, timeZone: string): Date {\r\n const normalizedDate = typeof date === 'number' ? new Date(date) : date;\r\n return endOfDay(new TZDate(normalizedDate, timeZone));\r\n }\r\n\r\n /**\r\n * Checks if a date string is valid according to the specified format.\r\n * @param dateString The date string to validate.\r\n * @param dateFormat Optional date format (default: ISO_8601_FORMAT).\r\n * @returns True if the date is valid, false otherwise.\r\n */\r\n static isValidDate(dateString: string, dateFormat?: string): boolean {\r\n try {\r\n DateUtil.readDate(dateString, dateFormat);\r\n return true;\r\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\r\n } catch (error) {\r\n return false;\r\n }\r\n }\r\n\r\n /**\r\n * Calculates the absolute number of days between two dates or timestamps.\r\n * @param firstDate The first date or timestamp.\r\n * @param secondDate The second date or timestamp.\r\n * @returns Number of days between the two dates.\r\n */\r\n static daysInBetween(firstDate: Date, secondDate: Date): number;\r\n static daysInBetween(firstDate: number, secondDate: number): number;\r\n static daysInBetween(firstDate: Date | number, secondDate: Date | number): number {\r\n const first = typeof firstDate === 'number' ? new Date(firstDate) : firstDate;\r\n const second = typeof secondDate === 'number' ? new Date(secondDate) : secondDate;\r\n return Math.abs(differenceInCalendarDays(first, second));\r\n }\r\n\r\n /**\r\n * Calculates the absolute number of months between two dates or timestamps.\r\n * @param firstDate The first date or timestamp.\r\n * @param secondDate The second date or timestamp.\r\n * @returns The number of months between the two dates.\r\n */\r\n static monthsInBetween(firstDate: Date, secondDate: Date): number;\r\n static monthsInBetween(firstDate: number, secondDate: number): number;\r\n static monthsInBetween(firstDate: Date | number, secondDate: Date | number): number {\r\n const first = typeof firstDate === 'number' ? new Date(firstDate) : firstDate;\r\n const second = typeof secondDate === 'number' ? new Date(secondDate) : secondDate;\r\n return Math.abs(differenceInCalendarMonths(first, second));\r\n }\r\n\r\n /**\r\n * Calculates the absolute number of years between two dates or timestamps.\r\n * @param firstDate The first date or timestamp.\r\n * @param secondDate The second date or timestamp.\r\n * @returns The number of years between the two dates.\r\n */\r\n static anniversaryYearsInBetween(firstDate: Date, secondDate: Date): number;\r\n static anniversaryYearsInBetween(firstDate: number, secondDate: number): number;\r\n static anniversaryYearsInBetween(firstDate: Date | number, secondDate: Date | number): number {\r\n const first = typeof firstDate === 'number' ? new Date(firstDate) : firstDate;\r\n const second = typeof secondDate === 'number' ? new Date(secondDate) : secondDate;\r\n return Math.abs(differenceInYears(first, second));\r\n }\r\n\r\n /**\r\n * Calculates the absolute number of years between two dates or timestamps.\r\n * @param firstDate The first date or timestamp.\r\n * @param secondDate The second date or timestamp.\r\n * @returns The number of years between the two dates.\r\n */\r\n static yearsInBetween(firstDate: Date, secondDate: Date): number;\r\n static yearsInBetween(firstDate: number, secondDate: number): number;\r\n static yearsInBetween(firstDate: Date | number, secondDate: Date | number): number {\r\n const first = typeof firstDate === 'number' ? new Date(firstDate) : firstDate;\r\n const second = typeof secondDate === 'number' ? new Date(secondDate) : secondDate;\r\n return Math.abs(differenceInCalendarYears(first, second));\r\n }\r\n\r\n /**\r\n * Compares two dates or timestamps.\r\n * @param firstDate The first date or timestamp.\r\n * @param secondDate The second date or timestamp.\r\n * @returns `-1` if the first date is earlier, `1` if it is later, or `0` if the two dates are equal.\r\n */\r\n static compareDates(firstDate: Date, secondDate: Date): -1 | 0 | 1;\r\n static compareDates(firstDate: number, secondDate: number): -1 | 0 | 1;\r\n static compareDates(firstDate: Date | number, secondDate: Date | number): -1 | 0 | 1 {\r\n const first = typeof firstDate === 'number' ? new Date(firstDate) : firstDate;\r\n const second = typeof secondDate === 'number' ? new Date(secondDate) : secondDate;\r\n\r\n return compareAsc(first, second) as -1 | 0 | 1;\r\n }\r\n\r\n /**\r\n * Adds a duration (e.g., days, months, years) to a given date or timestamp.\r\n * @param date The base date or timestamp to which the duration will be added.\r\n * @param duration An object specifying the duration (e.g., `{ days: 1, months: 2 }`).\r\n * @returns A new Date object with the duration added.\r\n */\r\n static addDuration(date: Date, duration: Duration): Date;\r\n static addDuration(date: number, duration: Duration): Date;\r\n static addDuration(date: Date | number, duration: Duration): Date {\r\n const normalizedDate = typeof date === 'number' ? new Date(date) : date;\r\n return add(normalizedDate, duration);\r\n }\r\n\r\n /**\r\n * Determines whether a given year is a leap year.\r\n * @param year The year to check.\r\n * @returns `true` if the year is a leap year, otherwise `false`.\r\n */\r\n static isLeapYear(year: number): boolean {\r\n return isLeapYear(DateUtil.readDate(year.toString(), 'yyyy'));\r\n }\r\n\r\n\r\n /**\r\n * Converts a timestamp (in milliseconds) to a Date object.\r\n * @param milliseconds The timestamp in milliseconds since the Unix epoch.\r\n * @returns A Date object representing the provided timestamp.\r\n */\r\n static fromMillis(milliseconds: number): Date {\r\n return new Date(milliseconds);\r\n }\r\n\r\n /**\r\n * Converts a Date object to a timestamp (in milliseconds).\r\n * @param date The Date object to convert.\r\n * @returns The timestamp in milliseconds since the Unix epoch.\r\n */\r\n static toMillis(date: Date): number {\r\n return date.getTime();\r\n }\r\n}\r\n\r\n/**\r\n * Utility class for handling date operations in a fixed time zone.\r\n * Extends BaseDateUtil with default settings for the 'Asia/Kolkata' time zone.\r\n */\r\nexport class DateUtil extends BaseDateUtil {\r\n /** Default time zone used for all date operations. */\r\n static readonly TIMEZONE = 'Asia/Kolkata';\r\n /**\r\n * Gets the current date and time in the default time zone.\r\n * @returns Current date and time as a Date object.\r\n */\r\n static override now(): Date {\r\n return BaseDateUtil.now(this.TIMEZONE);\r\n }\r\n\r\n /**\r\n * Parses a date string into a Date object using the default time zone.\r\n * @param dateString The date string to parse.\r\n * @param dateFormat Optional format of the date string (default: ISO_8601_FORMAT).\r\n * @throws {IllegalArgumentException} If the date string is invalid.\r\n * @returns Parsed Date object.\r\n */\r\n static override readDate(dateString: string, dateFormat?: string): Date {\r\n return BaseDateUtil.readDate(dateString, dateFormat || DateUtil.ISO_8601_FORMAT, this.TIMEZONE);\r\n }\r\n\r\n /**\r\n * Formats a Date or timestamp into a string using the default time zone.\r\n * @param date The date or timestamp to format.\r\n * @param dateFormat Optional desired output format (default: ISO_8601_FORMAT).\r\n * @returns Formatted date string.\r\n */\r\n static override printDate(date: Date, dateFormat?: string): string;\r\n static override printDate(date: number, dateFormat?: string): string;\r\n static override printDate(date: Date | number, dateFormat = DateUtil.ISO_8601_FORMAT): string {\r\n const normalizedDate = typeof date === 'number' ? new Date(date) : date;\r\n return BaseDateUtil.printDate(normalizedDate, this.TIMEZONE, dateFormat);\r\n }\r\n\r\n /**\r\n * Gets the start of the day for a given date or timestamp using the default time zone.\r\n * @param date The date or timestamp to calculate from.\r\n * @returns A Date object representing the start of the day.\r\n */\r\n static override getStartOfDay(date: Date): Date;\r\n static override getStartOfDay(date: number): Date;\r\n static override getStartOfDay(date: Date | number): Date {\r\n const normalizedDate = typeof date === 'number' ? new Date(date) : date;\r\n return BaseDateUtil.getStartOfDay(normalizedDate, this.TIMEZONE);\r\n }\r\n\r\n /**\r\n * Gets the end of the day for a given date or timestamp using the default time zone.\r\n * @param date The date or timestamp to calculate from.\r\n * @returns A Date object representing the end of the day.\r\n */\r\n static override getEndOfDay(date: Date): Date;\r\n static override getEndOfDay(date: number): Date;\r\n static override getEndOfDay(date: Date | number): Date {\r\n const normalizedDate = typeof date === 'number' ? new Date(date) : date;\r\n return BaseDateUtil.getEndOfDay(normalizedDate, this.TIMEZONE);\r\n }\r\n}\r\n"],"names":[],"mappings":";;;;AAIA;;;AAGG;AACH,MAAM,YAAY,CAAA;aAEE,IAAe,CAAA,eAAA,GAAG,8BAA8B,CAAA;AAEhE;;;;AAIG;IACO,OAAO,GAAG,CAAC,QAAgB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78,6 +78,11 @@ class BaseDateUtil {
|
|
|
78
78
|
const second = typeof secondDate === 'number' ? new Date(secondDate) : secondDate;
|
|
79
79
|
return Math.abs(dateFns.differenceInCalendarMonths(first, second));
|
|
80
80
|
}
|
|
81
|
+
static anniversaryYearsInBetween(firstDate, secondDate) {
|
|
82
|
+
const first = typeof firstDate === 'number' ? new Date(firstDate) : firstDate;
|
|
83
|
+
const second = typeof secondDate === 'number' ? new Date(secondDate) : secondDate;
|
|
84
|
+
return Math.abs(dateFns.differenceInYears(first, second));
|
|
85
|
+
}
|
|
81
86
|
static yearsInBetween(firstDate, secondDate) {
|
|
82
87
|
const first = typeof firstDate === 'number' ? new Date(firstDate) : firstDate;
|
|
83
88
|
const second = typeof secondDate === 'number' ? new Date(secondDate) : secondDate;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DateUtil.cjs","sources":["../../../../src/utils/date/DateUtil.ts"],"sourcesContent":["import { TZDate } from '@date-fns/tz';\r\nimport { add, compareAsc, differenceInCalendarDays, differenceInCalendarMonths, differenceInCalendarYears, Duration, endOfDay, format, isLeapYear, isValid, parse, startOfDay } from 'date-fns';\r\nimport { IllegalArgumentException } from '../../exceptions';\r\n\r\n/**\r\n * Utility class for date and time operations. Provides methods for parsing,\r\n * formatting, and performing calculations with dates and times.\r\n */\r\nclass BaseDateUtil {\r\n\r\n static readonly ISO_8601_FORMAT = \"yyyy-MM-dd'T'HH:mm:ss.SSSxxx\"\r\n\r\n /**\r\n * Gets the current date and time in the specified time zone.\r\n * @param timeZone The IANA time zone identifier.\r\n * @returns Current date and time in the specified time zone.\r\n */\r\n protected static now(timeZone: string): Date {\r\n return new TZDate(new Date(), timeZone);\r\n }\r\n\r\n /**\r\n * Gets the current time in milliseconds since the Unix epoch.\r\n * @returns Current time in milliseconds.\r\n */\r\n static nowInMillis(): number {\r\n return Date.now();\r\n }\r\n\r\n /**\r\n * Parses a date string into a Date object.\r\n * @param dateString The date string to parse.\r\n * @param dateFormat The format of the date string (default: ISO_8601_FORMAT).\r\n * @param timeZone The IANA time zone identifier.\r\n * @throws {IllegalArgumentException} If the date string is invalid.\r\n * @returns Parsed Date object.\r\n */\r\n protected static readDate(dateString: string, dateFormat = this.ISO_8601_FORMAT, timeZone: string): Date {\r\n const date = parse(dateString, dateFormat, TZDate.tz(timeZone));\r\n if (!isValid(date)) {\r\n throw new IllegalArgumentException('Invalid date string or date format');\r\n }\r\n return date;\r\n }\r\n\r\n /**\r\n * Formats a Date or timestamp into a string.\r\n * @param date The date or timestamp to format.\r\n * @param timeZone The IANA time zone identifier.\r\n * @param dateFormat The desired output format (default: ISO_8601_FORMAT).\r\n * @returns Formatted date string.\r\n */\r\n protected static printDate(date: Date, timeZone: string, dateFormat?: string): string;\r\n protected static printDate(date: number, timeZone: string, dateFormat?: string): string;\r\n protected static printDate(date: Date | number, timeZone: string, dateFormat: string = this.ISO_8601_FORMAT): string {\r\n const normalizedDate = typeof date === 'number' ? new Date(date) : date;\r\n return format(new TZDate(normalizedDate, timeZone), dateFormat);\r\n }\r\n\r\n /**\r\n * Gets the start of the day for a given date or timestamp.\r\n * @param date The date or timestamp to calculate from.\r\n * @param timeZone The IANA time zone identifier.\r\n * @returns The start of the day as a Date object.\r\n */\r\n protected static getStartOfDay(date: Date, timeZone: string): Date;\r\n protected static getStartOfDay(date: number, timeZone: string): Date;\r\n protected static getStartOfDay(date: Date | number, timeZone: string): Date {\r\n const normalizedDate = typeof date === 'number' ? new Date(date) : date;\r\n return startOfDay(new TZDate(normalizedDate, timeZone));\r\n }\r\n\r\n /**\r\n * Gets the end of the day for a given date or timestamp.\r\n * @param date The date or timestamp to calculate from.\r\n * @param timeZone The IANA time zone identifier.\r\n * @returns The end of the day as a Date object.\r\n */\r\n protected static getEndOfDay(date: Date, timeZone: string): Date;\r\n protected static getEndOfDay(date: number, timeZone: string): Date;\r\n protected static getEndOfDay(date: Date | number, timeZone: string): Date {\r\n const normalizedDate = typeof date === 'number' ? new Date(date) : date;\r\n return endOfDay(new TZDate(normalizedDate, timeZone));\r\n }\r\n\r\n /**\r\n * Checks if a date string is valid according to the specified format.\r\n * @param dateString The date string to validate.\r\n * @param dateFormat Optional date format (default: ISO_8601_FORMAT).\r\n * @returns True if the date is valid, false otherwise.\r\n */\r\n static isValidDate(dateString: string, dateFormat?: string): boolean {\r\n try {\r\n DateUtil.readDate(dateString, dateFormat);\r\n return true;\r\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\r\n } catch (error) {\r\n return false;\r\n }\r\n }\r\n\r\n /**\r\n * Calculates the absolute number of days between two dates or timestamps.\r\n * @param firstDate The first date or timestamp.\r\n * @param secondDate The second date or timestamp.\r\n * @returns Number of days between the two dates.\r\n */\r\n static daysInBetween(firstDate: Date, secondDate: Date): number;\r\n static daysInBetween(firstDate: number, secondDate: number): number;\r\n static daysInBetween(firstDate: Date | number, secondDate: Date | number): number {\r\n const first = typeof firstDate === 'number' ? new Date(firstDate) : firstDate;\r\n const second = typeof secondDate === 'number' ? new Date(secondDate) : secondDate;\r\n return Math.abs(differenceInCalendarDays(first, second));\r\n }\r\n\r\n /**\r\n * Calculates the absolute number of months between two dates or timestamps.\r\n * @param firstDate The first date or timestamp.\r\n * @param secondDate The second date or timestamp.\r\n * @returns The number of months between the two dates.\r\n */\r\n static monthsInBetween(firstDate: Date, secondDate: Date): number;\r\n static monthsInBetween(firstDate: number, secondDate: number): number;\r\n static monthsInBetween(firstDate: Date | number, secondDate: Date | number): number {\r\n const first = typeof firstDate === 'number' ? new Date(firstDate) : firstDate;\r\n const second = typeof secondDate === 'number' ? new Date(secondDate) : secondDate;\r\n return Math.abs(differenceInCalendarMonths(first, second));\r\n }\r\n\r\n /**\r\n * Calculates the absolute number of years between two dates or timestamps.\r\n * @param firstDate The first date or timestamp.\r\n * @param secondDate The second date or timestamp.\r\n * @returns The number of years between the two dates.\r\n */\r\n static yearsInBetween(firstDate: Date, secondDate: Date): number;\r\n static yearsInBetween(firstDate: number, secondDate: number): number;\r\n static yearsInBetween(firstDate: Date | number, secondDate: Date | number): number {\r\n const first = typeof firstDate === 'number' ? new Date(firstDate) : firstDate;\r\n const second = typeof secondDate === 'number' ? new Date(secondDate) : secondDate;\r\n return Math.abs(differenceInCalendarYears(first, second));\r\n }\r\n\r\n /**\r\n * Compares two dates or timestamps.\r\n * @param firstDate The first date or timestamp.\r\n * @param secondDate The second date or timestamp.\r\n * @returns `-1` if the first date is earlier, `1` if it is later, or `0` if the two dates are equal.\r\n */\r\n static compareDates(firstDate: Date, secondDate: Date): -1 | 0 | 1;\r\n static compareDates(firstDate: number, secondDate: number): -1 | 0 | 1;\r\n static compareDates(firstDate: Date | number, secondDate: Date | number): -1 | 0 | 1 {\r\n const first = typeof firstDate === 'number' ? new Date(firstDate) : firstDate;\r\n const second = typeof secondDate === 'number' ? new Date(secondDate) : secondDate;\r\n\r\n return compareAsc(first, second) as -1 | 0 | 1;\r\n }\r\n\r\n /**\r\n * Adds a duration (e.g., days, months, years) to a given date or timestamp.\r\n * @param date The base date or timestamp to which the duration will be added.\r\n * @param duration An object specifying the duration (e.g., `{ days: 1, months: 2 }`).\r\n * @returns A new Date object with the duration added.\r\n */\r\n static addDuration(date: Date, duration: Duration): Date;\r\n static addDuration(date: number, duration: Duration): Date;\r\n static addDuration(date: Date | number, duration: Duration): Date {\r\n const normalizedDate = typeof date === 'number' ? new Date(date) : date;\r\n return add(normalizedDate, duration);\r\n }\r\n\r\n /**\r\n * Determines whether a given year is a leap year.\r\n * @param year The year to check.\r\n * @returns `true` if the year is a leap year, otherwise `false`.\r\n */\r\n static isLeapYear(year: number): boolean {\r\n return isLeapYear(DateUtil.readDate(year.toString(), 'yyyy'));\r\n }\r\n\r\n\r\n /**\r\n * Converts a timestamp (in milliseconds) to a Date object.\r\n * @param milliseconds The timestamp in milliseconds since the Unix epoch.\r\n * @returns A Date object representing the provided timestamp.\r\n */\r\n static fromMillis(milliseconds: number): Date {\r\n return new Date(milliseconds);\r\n }\r\n\r\n /**\r\n * Converts a Date object to a timestamp (in milliseconds).\r\n * @param date The Date object to convert.\r\n * @returns The timestamp in milliseconds since the Unix epoch.\r\n */\r\n static toMillis(date: Date): number {\r\n return date.getTime();\r\n }\r\n}\r\n\r\n/**\r\n * Utility class for handling date operations in a fixed time zone.\r\n * Extends BaseDateUtil with default settings for the 'Asia/Kolkata' time zone.\r\n */\r\nexport class DateUtil extends BaseDateUtil {\r\n /** Default time zone used for all date operations. */\r\n static readonly TIMEZONE = 'Asia/Kolkata';\r\n /**\r\n * Gets the current date and time in the default time zone.\r\n * @returns Current date and time as a Date object.\r\n */\r\n static override now(): Date {\r\n return BaseDateUtil.now(this.TIMEZONE);\r\n }\r\n\r\n /**\r\n * Parses a date string into a Date object using the default time zone.\r\n * @param dateString The date string to parse.\r\n * @param dateFormat Optional format of the date string (default: ISO_8601_FORMAT).\r\n * @throws {IllegalArgumentException} If the date string is invalid.\r\n * @returns Parsed Date object.\r\n */\r\n static override readDate(dateString: string, dateFormat?: string): Date {\r\n return BaseDateUtil.readDate(dateString, dateFormat || DateUtil.ISO_8601_FORMAT, this.TIMEZONE);\r\n }\r\n\r\n /**\r\n * Formats a Date or timestamp into a string using the default time zone.\r\n * @param date The date or timestamp to format.\r\n * @param dateFormat Optional desired output format (default: ISO_8601_FORMAT).\r\n * @returns Formatted date string.\r\n */\r\n static override printDate(date: Date, dateFormat?: string): string;\r\n static override printDate(date: number, dateFormat?: string): string;\r\n static override printDate(date: Date | number, dateFormat = DateUtil.ISO_8601_FORMAT): string {\r\n const normalizedDate = typeof date === 'number' ? new Date(date) : date;\r\n return BaseDateUtil.printDate(normalizedDate, this.TIMEZONE, dateFormat);\r\n }\r\n\r\n /**\r\n * Gets the start of the day for a given date or timestamp using the default time zone.\r\n * @param date The date or timestamp to calculate from.\r\n * @returns A Date object representing the start of the day.\r\n */\r\n static override getStartOfDay(date: Date): Date;\r\n static override getStartOfDay(date: number): Date;\r\n static override getStartOfDay(date: Date | number): Date {\r\n const normalizedDate = typeof date === 'number' ? new Date(date) : date;\r\n return BaseDateUtil.getStartOfDay(normalizedDate, this.TIMEZONE);\r\n }\r\n\r\n /**\r\n * Gets the end of the day for a given date or timestamp using the default time zone.\r\n * @param date The date or timestamp to calculate from.\r\n * @returns A Date object representing the end of the day.\r\n */\r\n static override getEndOfDay(date: Date): Date;\r\n static override getEndOfDay(date: number): Date;\r\n static override getEndOfDay(date: Date | number): Date {\r\n const normalizedDate = typeof date === 'number' ? new Date(date) : date;\r\n return BaseDateUtil.getEndOfDay(normalizedDate, this.TIMEZONE);\r\n }\r\n}\r\n"],"names":["TZDate","parse","isValid","IllegalArgumentException","format","startOfDay","endOfDay","differenceInCalendarDays","differenceInCalendarMonths","differenceInCalendarYears","compareAsc","add","isLeapYear"],"mappings":";;;;;;AAIA;;;AAGG;AACH,MAAM,YAAY,CAAA;aAEE,IAAe,CAAA,eAAA,GAAG,8BAA8B,CAAA;AAEhE;;;;AAIG;IACO,OAAO,GAAG,CAAC,QAAgB,EAAA;QACjC,OAAO,IAAIA,SAAM,CAAC,IAAI,IAAI,EAAE,EAAE,QAAQ,CAAC;;AAG3C;;;AAGG;AACH,IAAA,OAAO,WAAW,GAAA;AACd,QAAA,OAAO,IAAI,CAAC,GAAG,EAAE;;AAGrB;;;;;;;AAOG;IACO,OAAO,QAAQ,CAAC,UAAkB,EAAE,UAAU,GAAG,IAAI,CAAC,eAAe,EAAE,QAAgB,EAAA;AAC7F,QAAA,MAAM,IAAI,GAAGC,aAAK,CAAC,UAAU,EAAE,UAAU,EAAED,SAAM,CAAC,EAAE,CAAC,QAAQ,CAAC,CAAC;AAC/D,QAAA,IAAI,CAACE,eAAO,CAAC,IAAI,CAAC,EAAE;AAChB,YAAA,MAAM,IAAIC,iDAAwB,CAAC,oCAAoC,CAAC;;AAE5E,QAAA,OAAO,IAAI;;IAYL,OAAO,SAAS,CAAC,IAAmB,EAAE,QAAgB,EAAE,UAAA,GAAqB,IAAI,CAAC,eAAe,EAAA;AACvG,QAAA,MAAM,cAAc,G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EAAE,IAAI,CAAC,QAAQ,CAAC;;IAUpE,OAAgB,WAAW,CAAC,IAAmB,EAAA;AAC3C,QAAA,MAAM,cAAc,GAAG,OAAO,IAAI,KAAK,QAAQ,GAAG,IAAI,IAAI,CAAC,IAAI,CAAC,GAAG,IAAI;QACvE,OAAO,YAAY,CAAC,WAAW,CAAC,cAAc,EAAE,IAAI,CAAC,QAAQ,CAAC;;;;;;"}
|
|
1
|
+
{"version":3,"file":"DateUtil.cjs","sources":["../../../../src/utils/date/DateUtil.ts"],"sourcesContent":["import { TZDate } from '@date-fns/tz';\r\nimport { add, compareAsc, differenceInCalendarDays, differenceInCalendarMonths, differenceInCalendarYears, differenceInYears, Duration, endOfDay, format, isLeapYear, isValid, parse, startOfDay } from 'date-fns';\r\nimport { IllegalArgumentException } from '../../exceptions';\r\n\r\n/**\r\n * Utility class for date and time operations. Provides methods for parsing,\r\n * formatting, and performing calculations with dates and times.\r\n */\r\nclass BaseDateUtil {\r\n\r\n static readonly ISO_8601_FORMAT = \"yyyy-MM-dd'T'HH:mm:ss.SSSxxx\"\r\n\r\n /**\r\n * Gets the current date and time in the specified time zone.\r\n * @param timeZone The IANA time zone identifier.\r\n * @returns Current date and time in the specified time zone.\r\n */\r\n protected static now(timeZone: string): Date {\r\n return new TZDate(new Date(), timeZone);\r\n }\r\n\r\n /**\r\n * Gets the current time in milliseconds since the Unix epoch.\r\n * @returns Current time in milliseconds.\r\n */\r\n static nowInMillis(): number {\r\n return Date.now();\r\n }\r\n\r\n /**\r\n * Parses a date string into a Date object.\r\n * @param dateString The date string to parse.\r\n * @param dateFormat The format of the date string (default: ISO_8601_FORMAT).\r\n * @param timeZone The IANA time zone identifier.\r\n * @throws {IllegalArgumentException} If the date string is invalid.\r\n * @returns Parsed Date object.\r\n */\r\n protected static readDate(dateString: string, dateFormat = this.ISO_8601_FORMAT, timeZone: string): Date {\r\n const date = parse(dateString, dateFormat, TZDate.tz(timeZone));\r\n if (!isValid(date)) {\r\n throw new IllegalArgumentException('Invalid date string or date format');\r\n }\r\n return date;\r\n }\r\n\r\n /**\r\n * Formats a Date or timestamp into a string.\r\n * @param date The date or timestamp to format.\r\n * @param timeZone The IANA time zone identifier.\r\n * @param dateFormat The desired output format (default: ISO_8601_FORMAT).\r\n * @returns Formatted date string.\r\n */\r\n protected static printDate(date: Date, timeZone: string, dateFormat?: string): string;\r\n protected static printDate(date: number, timeZone: string, dateFormat?: string): string;\r\n protected static printDate(date: Date | number, timeZone: string, dateFormat: string = this.ISO_8601_FORMAT): string {\r\n const normalizedDate = typeof date === 'number' ? new Date(date) : date;\r\n return format(new TZDate(normalizedDate, timeZone), dateFormat);\r\n }\r\n\r\n /**\r\n * Gets the start of the day for a given date or timestamp.\r\n * @param date The date or timestamp to calculate from.\r\n * @param timeZone The IANA time zone identifier.\r\n * @returns The start of the day as a Date object.\r\n */\r\n protected static getStartOfDay(date: Date, timeZone: string): Date;\r\n protected static getStartOfDay(date: number, timeZone: string): Date;\r\n protected static getStartOfDay(date: Date | number, timeZone: string): Date {\r\n const normalizedDate = typeof date === 'number' ? new Date(date) : date;\r\n return startOfDay(new TZDate(normalizedDate, timeZone));\r\n }\r\n\r\n /**\r\n * Gets the end of the day for a given date or timestamp.\r\n * @param date The date or timestamp to calculate from.\r\n * @param timeZone The IANA time zone identifier.\r\n * @returns The end of the day as a Date object.\r\n */\r\n protected static getEndOfDay(date: Date, timeZone: string): Date;\r\n protected static getEndOfDay(date: number, timeZone: string): Date;\r\n protected static getEndOfDay(date: Date | number, timeZone: string): Date {\r\n const normalizedDate = typeof date === 'number' ? new Date(date) : date;\r\n return endOfDay(new TZDate(normalizedDate, timeZone));\r\n }\r\n\r\n /**\r\n * Checks if a date string is valid according to the specified format.\r\n * @param dateString The date string to validate.\r\n * @param dateFormat Optional date format (default: ISO_8601_FORMAT).\r\n * @returns True if the date is valid, false otherwise.\r\n */\r\n static isValidDate(dateString: string, dateFormat?: string): boolean {\r\n try {\r\n DateUtil.readDate(dateString, dateFormat);\r\n return true;\r\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\r\n } catch (error) {\r\n return false;\r\n }\r\n }\r\n\r\n /**\r\n * Calculates the absolute number of days between two dates or timestamps.\r\n * @param firstDate The first date or timestamp.\r\n * @param secondDate The second date or timestamp.\r\n * @returns Number of days between the two dates.\r\n */\r\n static daysInBetween(firstDate: Date, secondDate: Date): number;\r\n static daysInBetween(firstDate: number, secondDate: number): number;\r\n static daysInBetween(firstDate: Date | number, secondDate: Date | number): number {\r\n const first = typeof firstDate === 'number' ? new Date(firstDate) : firstDate;\r\n const second = typeof secondDate === 'number' ? new Date(secondDate) : secondDate;\r\n return Math.abs(differenceInCalendarDays(first, second));\r\n }\r\n\r\n /**\r\n * Calculates the absolute number of months between two dates or timestamps.\r\n * @param firstDate The first date or timestamp.\r\n * @param secondDate The second date or timestamp.\r\n * @returns The number of months between the two dates.\r\n */\r\n static monthsInBetween(firstDate: Date, secondDate: Date): number;\r\n static monthsInBetween(firstDate: number, secondDate: number): number;\r\n static monthsInBetween(firstDate: Date | number, secondDate: Date | number): number {\r\n const first = typeof firstDate === 'number' ? new Date(firstDate) : firstDate;\r\n const second = typeof secondDate === 'number' ? new Date(secondDate) : secondDate;\r\n return Math.abs(differenceInCalendarMonths(first, second));\r\n }\r\n\r\n /**\r\n * Calculates the absolute number of years between two dates or timestamps.\r\n * @param firstDate The first date or timestamp.\r\n * @param secondDate The second date or timestamp.\r\n * @returns The number of years between the two dates.\r\n */\r\n static anniversaryYearsInBetween(firstDate: Date, secondDate: Date): number;\r\n static anniversaryYearsInBetween(firstDate: number, secondDate: number): number;\r\n static anniversaryYearsInBetween(firstDate: Date | number, secondDate: Date | number): number {\r\n const first = typeof firstDate === 'number' ? new Date(firstDate) : firstDate;\r\n const second = typeof secondDate === 'number' ? new Date(secondDate) : secondDate;\r\n return Math.abs(differenceInYears(first, second));\r\n }\r\n\r\n /**\r\n * Calculates the absolute number of years between two dates or timestamps.\r\n * @param firstDate The first date or timestamp.\r\n * @param secondDate The second date or timestamp.\r\n * @returns The number of years between the two dates.\r\n */\r\n static yearsInBetween(firstDate: Date, secondDate: Date): number;\r\n static yearsInBetween(firstDate: number, secondDate: number): number;\r\n static yearsInBetween(firstDate: Date | number, secondDate: Date | number): number {\r\n const first = typeof firstDate === 'number' ? new Date(firstDate) : firstDate;\r\n const second = typeof secondDate === 'number' ? new Date(secondDate) : secondDate;\r\n return Math.abs(differenceInCalendarYears(first, second));\r\n }\r\n\r\n /**\r\n * Compares two dates or timestamps.\r\n * @param firstDate The first date or timestamp.\r\n * @param secondDate The second date or timestamp.\r\n * @returns `-1` if the first date is earlier, `1` if it is later, or `0` if the two dates are equal.\r\n */\r\n static compareDates(firstDate: Date, secondDate: Date): -1 | 0 | 1;\r\n static compareDates(firstDate: number, secondDate: number): -1 | 0 | 1;\r\n static compareDates(firstDate: Date | number, secondDate: Date | number): -1 | 0 | 1 {\r\n const first = typeof firstDate === 'number' ? new Date(firstDate) : firstDate;\r\n const second = typeof secondDate === 'number' ? new Date(secondDate) : secondDate;\r\n\r\n return compareAsc(first, second) as -1 | 0 | 1;\r\n }\r\n\r\n /**\r\n * Adds a duration (e.g., days, months, years) to a given date or timestamp.\r\n * @param date The base date or timestamp to which the duration will be added.\r\n * @param duration An object specifying the duration (e.g., `{ days: 1, months: 2 }`).\r\n * @returns A new Date object with the duration added.\r\n */\r\n static addDuration(date: Date, duration: Duration): Date;\r\n static addDuration(date: number, duration: Duration): Date;\r\n static addDuration(date: Date | number, duration: Duration): Date {\r\n const normalizedDate = typeof date === 'number' ? new Date(date) : date;\r\n return add(normalizedDate, duration);\r\n }\r\n\r\n /**\r\n * Determines whether a given year is a leap year.\r\n * @param year The year to check.\r\n * @returns `true` if the year is a leap year, otherwise `false`.\r\n */\r\n static isLeapYear(year: number): boolean {\r\n return isLeapYear(DateUtil.readDate(year.toString(), 'yyyy'));\r\n }\r\n\r\n\r\n /**\r\n * Converts a timestamp (in milliseconds) to a Date object.\r\n * @param milliseconds The timestamp in milliseconds since the Unix epoch.\r\n * @returns A Date object representing the provided timestamp.\r\n */\r\n static fromMillis(milliseconds: number): Date {\r\n return new Date(milliseconds);\r\n }\r\n\r\n /**\r\n * Converts a Date object to a timestamp (in milliseconds).\r\n * @param date The Date object to convert.\r\n * @returns The timestamp in milliseconds since the Unix epoch.\r\n */\r\n static toMillis(date: Date): number {\r\n return date.getTime();\r\n }\r\n}\r\n\r\n/**\r\n * Utility class for handling date operations in a fixed time zone.\r\n * Extends BaseDateUtil with default settings for the 'Asia/Kolkata' time zone.\r\n */\r\nexport class DateUtil extends BaseDateUtil {\r\n /** Default time zone used for all date operations. */\r\n static readonly TIMEZONE = 'Asia/Kolkata';\r\n /**\r\n * Gets the current date and time in the default time zone.\r\n * @returns Current date and time as a Date object.\r\n */\r\n static override now(): Date {\r\n return BaseDateUtil.now(this.TIMEZONE);\r\n }\r\n\r\n /**\r\n * Parses a date string into a Date object using the default time zone.\r\n * @param dateString The date string to parse.\r\n * @param dateFormat Optional format of the date string (default: ISO_8601_FORMAT).\r\n * @throws {IllegalArgumentException} If the date string is invalid.\r\n * @returns Parsed Date object.\r\n */\r\n static override readDate(dateString: string, dateFormat?: string): Date {\r\n return BaseDateUtil.readDate(dateString, dateFormat || DateUtil.ISO_8601_FORMAT, this.TIMEZONE);\r\n }\r\n\r\n /**\r\n * Formats a Date or timestamp into a string using the default time zone.\r\n * @param date The date or timestamp to format.\r\n * @param dateFormat Optional desired output format (default: ISO_8601_FORMAT).\r\n * @returns Formatted date string.\r\n */\r\n static override printDate(date: Date, dateFormat?: string): string;\r\n static override printDate(date: number, dateFormat?: string): string;\r\n static override printDate(date: Date | number, dateFormat = DateUtil.ISO_8601_FORMAT): string {\r\n const normalizedDate = typeof date === 'number' ? new Date(date) : date;\r\n return BaseDateUtil.printDate(normalizedDate, this.TIMEZONE, dateFormat);\r\n }\r\n\r\n /**\r\n * Gets the start of the day for a given date or timestamp using the default time zone.\r\n * @param date The date or timestamp to calculate from.\r\n * @returns A Date object representing the start of the day.\r\n */\r\n static override getStartOfDay(date: Date): Date;\r\n static override getStartOfDay(date: number): Date;\r\n static override getStartOfDay(date: Date | number): Date {\r\n const normalizedDate = typeof date === 'number' ? new Date(date) : date;\r\n return BaseDateUtil.getStartOfDay(normalizedDate, this.TIMEZONE);\r\n }\r\n\r\n /**\r\n * Gets the end of the day for a given date or timestamp using the default time zone.\r\n * @param date The date or timestamp to calculate from.\r\n * @returns A Date object representing the end of the day.\r\n */\r\n static override getEndOfDay(date: Date): Date;\r\n static override getEndOfDay(date: number): Date;\r\n static override getEndOfDay(date: Date | number): Date {\r\n const normalizedDate = typeof date === 'number' ? new Date(date) : date;\r\n return BaseDateUtil.getEndOfDay(normalizedDate, this.TIMEZONE);\r\n }\r\n}\r\n"],"names":["TZDate","parse","isValid","IllegalArgumentException","format","startOfDay","endOfDay","differenceInCalendarDays","differenceInCalendarMonths","differenceInYears","differenceInCalendarYears","compareAsc","add","isLeapYear"],"mappings":";;;;;;AAIA;;;AAGG;AACH,MAAM,YAAY,CAAA;aAEE,IAAe,CAAA,eAAA,GAAG,8BAA8B,CAAA;AAEhE;;;;AAIG;IACO,OAAO,GAAG,CAAC,QAAgB,EAAA;QACjC,OAAO,IAAIA,SAAM,CAAC,IAAI,IAAI,EAAE,EAAE,QAAQ,CAAC;;AAG3C;;;AAGG;AACH,IAAA,OAAO,WAAW,GAAA;AACd,QAAA,OAAO,IAAI,CAAC,GAAG,EAAE;;AAGrB;;;;;;;AAOG;IACO,OAAO,QAAQ,CAAC,UAAkB,EAAE,UAAU,GAAG,IAAI,CAAC,eAAe,EAAE,QAAgB,EAAA;AAC7F,QAAA,MAAM,IAAI,GAAGC,aAAK,CAAC,UAAU,EAAE,UAAU,EAAED,SAAM,CAAC,EAAE,CAAC,QAAQ,CAAC,CAAC;AAC/D,QAAA,IAAI,CAACE,eAAO,CAAC,IAAI,CAAC,EAAE;AAChB,YAAA,MAAM,IAAIC,iDAAwB,CAAC,oCAAoC,CAAC;;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AE,IAAI,CAAC,QAAQ,CAAC;;IAWnG,OAAgB,SAAS,CAAC,IAAmB,EAAE,UAAU,GAAG,QAAQ,CAAC,eAAe,EAAA;AAChF,QAAA,MAAM,cAAc,GAAG,OAAO,IAAI,KAAK,QAAQ,GAAG,IAAI,IAAI,CAAC,IAAI,CAAC,GAAG,IAAI;AACvE,QAAA,OAAO,YAAY,CAAC,SAAS,CAAC,cAAc,EAAE,IAAI,CAAC,QAAQ,EAAE,UAAU,CAAC;;IAU5E,OAAgB,aAAa,CAAC,IAAmB,EAAA;AAC7C,QAAA,MAAM,cAAc,GAAG,OAAO,IAAI,KAAK,QAAQ,GAAG,IAAI,IAAI,CAAC,IAAI,CAAC,GAAG,IAAI;QACvE,OAAO,YAAY,CAAC,aAAa,CAAC,cAAc,EAAE,IAAI,CAAC,QAAQ,CAAC;;IAUpE,OAAgB,WAAW,CAAC,IAAmB,EAAA;AAC3C,QAAA,MAAM,cAAc,GAAG,OAAO,IAAI,KAAK,QAAQ,GAAG,IAAI,IAAI,CAAC,IAAI,CAAC,GAAG,IAAI;QACvE,OAAO,YAAY,CAAC,WAAW,CAAC,cAAc,EAAE,IAAI,CAAC,QAAQ,CAAC;;;;;;"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { TZDate } from '@date-fns/tz';
|
|
2
|
-
import { parse, isValid, format, startOfDay, endOfDay, differenceInCalendarDays, differenceInCalendarMonths, differenceInCalendarYears, compareAsc, add, isLeapYear } from 'date-fns';
|
|
2
|
+
import { parse, isValid, format, startOfDay, endOfDay, differenceInCalendarDays, differenceInCalendarMonths, differenceInYears, differenceInCalendarYears, compareAsc, add, isLeapYear } from 'date-fns';
|
|
3
3
|
import { IllegalArgumentException } from '../../exceptions/IllegalArgumentException.js';
|
|
4
4
|
|
|
5
5
|
/**
|
|
@@ -76,6 +76,11 @@ class BaseDateUtil {
|
|
|
76
76
|
const second = typeof secondDate === 'number' ? new Date(secondDate) : secondDate;
|
|
77
77
|
return Math.abs(differenceInCalendarMonths(first, second));
|
|
78
78
|
}
|
|
79
|
+
static anniversaryYearsInBetween(firstDate, secondDate) {
|
|
80
|
+
const first = typeof firstDate === 'number' ? new Date(firstDate) : firstDate;
|
|
81
|
+
const second = typeof secondDate === 'number' ? new Date(secondDate) : secondDate;
|
|
82
|
+
return Math.abs(differenceInYears(first, second));
|
|
83
|
+
}
|
|
79
84
|
static yearsInBetween(firstDate, secondDate) {
|
|
80
85
|
const first = typeof firstDate === 'number' ? new Date(firstDate) : firstDate;
|
|
81
86
|
const second = typeof secondDate === 'number' ? new Date(secondDate) : secondDate;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DateUtil.js","sources":["../../../../src/utils/date/DateUtil.ts"],"sourcesContent":["import { TZDate } from '@date-fns/tz';\r\nimport { add, compareAsc, differenceInCalendarDays, differenceInCalendarMonths, differenceInCalendarYears, Duration, endOfDay, format, isLeapYear, isValid, parse, startOfDay } from 'date-fns';\r\nimport { IllegalArgumentException } from '../../exceptions';\r\n\r\n/**\r\n * Utility class for date and time operations. Provides methods for parsing,\r\n * formatting, and performing calculations with dates and times.\r\n */\r\nclass BaseDateUtil {\r\n\r\n static readonly ISO_8601_FORMAT = \"yyyy-MM-dd'T'HH:mm:ss.SSSxxx\"\r\n\r\n /**\r\n * Gets the current date and time in the specified time zone.\r\n * @param timeZone The IANA time zone identifier.\r\n * @returns Current date and time in the specified time zone.\r\n */\r\n protected static now(timeZone: string): Date {\r\n return new TZDate(new Date(), timeZone);\r\n }\r\n\r\n /**\r\n * Gets the current time in milliseconds since the Unix epoch.\r\n * @returns Current time in milliseconds.\r\n */\r\n static nowInMillis(): number {\r\n return Date.now();\r\n }\r\n\r\n /**\r\n * Parses a date string into a Date object.\r\n * @param dateString The date string to parse.\r\n * @param dateFormat The format of the date string (default: ISO_8601_FORMAT).\r\n * @param timeZone The IANA time zone identifier.\r\n * @throws {IllegalArgumentException} If the date string is invalid.\r\n * @returns Parsed Date object.\r\n */\r\n protected static readDate(dateString: string, dateFormat = this.ISO_8601_FORMAT, timeZone: string): Date {\r\n const date = parse(dateString, dateFormat, TZDate.tz(timeZone));\r\n if (!isValid(date)) {\r\n throw new IllegalArgumentException('Invalid date string or date format');\r\n }\r\n return date;\r\n }\r\n\r\n /**\r\n * Formats a Date or timestamp into a string.\r\n * @param date The date or timestamp to format.\r\n * @param timeZone The IANA time zone identifier.\r\n * @param dateFormat The desired output format (default: ISO_8601_FORMAT).\r\n * @returns Formatted date string.\r\n */\r\n protected static printDate(date: Date, timeZone: string, dateFormat?: string): string;\r\n protected static printDate(date: number, timeZone: string, dateFormat?: string): string;\r\n protected static printDate(date: Date | number, timeZone: string, dateFormat: string = this.ISO_8601_FORMAT): string {\r\n const normalizedDate = typeof date === 'number' ? new Date(date) : date;\r\n return format(new TZDate(normalizedDate, timeZone), dateFormat);\r\n }\r\n\r\n /**\r\n * Gets the start of the day for a given date or timestamp.\r\n * @param date The date or timestamp to calculate from.\r\n * @param timeZone The IANA time zone identifier.\r\n * @returns The start of the day as a Date object.\r\n */\r\n protected static getStartOfDay(date: Date, timeZone: string): Date;\r\n protected static getStartOfDay(date: number, timeZone: string): Date;\r\n protected static getStartOfDay(date: Date | number, timeZone: string): Date {\r\n const normalizedDate = typeof date === 'number' ? new Date(date) : date;\r\n return startOfDay(new TZDate(normalizedDate, timeZone));\r\n }\r\n\r\n /**\r\n * Gets the end of the day for a given date or timestamp.\r\n * @param date The date or timestamp to calculate from.\r\n * @param timeZone The IANA time zone identifier.\r\n * @returns The end of the day as a Date object.\r\n */\r\n protected static getEndOfDay(date: Date, timeZone: string): Date;\r\n protected static getEndOfDay(date: number, timeZone: string): Date;\r\n protected static getEndOfDay(date: Date | number, timeZone: string): Date {\r\n const normalizedDate = typeof date === 'number' ? new Date(date) : date;\r\n return endOfDay(new TZDate(normalizedDate, timeZone));\r\n }\r\n\r\n /**\r\n * Checks if a date string is valid according to the specified format.\r\n * @param dateString The date string to validate.\r\n * @param dateFormat Optional date format (default: ISO_8601_FORMAT).\r\n * @returns True if the date is valid, false otherwise.\r\n */\r\n static isValidDate(dateString: string, dateFormat?: string): boolean {\r\n try {\r\n DateUtil.readDate(dateString, dateFormat);\r\n return true;\r\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\r\n } catch (error) {\r\n return false;\r\n }\r\n }\r\n\r\n /**\r\n * Calculates the absolute number of days between two dates or timestamps.\r\n * @param firstDate The first date or timestamp.\r\n * @param secondDate The second date or timestamp.\r\n * @returns Number of days between the two dates.\r\n */\r\n static daysInBetween(firstDate: Date, secondDate: Date): number;\r\n static daysInBetween(firstDate: number, secondDate: number): number;\r\n static daysInBetween(firstDate: Date | number, secondDate: Date | number): number {\r\n const first = typeof firstDate === 'number' ? new Date(firstDate) : firstDate;\r\n const second = typeof secondDate === 'number' ? new Date(secondDate) : secondDate;\r\n return Math.abs(differenceInCalendarDays(first, second));\r\n }\r\n\r\n /**\r\n * Calculates the absolute number of months between two dates or timestamps.\r\n * @param firstDate The first date or timestamp.\r\n * @param secondDate The second date or timestamp.\r\n * @returns The number of months between the two dates.\r\n */\r\n static monthsInBetween(firstDate: Date, secondDate: Date): number;\r\n static monthsInBetween(firstDate: number, secondDate: number): number;\r\n static monthsInBetween(firstDate: Date | number, secondDate: Date | number): number {\r\n const first = typeof firstDate === 'number' ? new Date(firstDate) : firstDate;\r\n const second = typeof secondDate === 'number' ? new Date(secondDate) : secondDate;\r\n return Math.abs(differenceInCalendarMonths(first, second));\r\n }\r\n\r\n /**\r\n * Calculates the absolute number of years between two dates or timestamps.\r\n * @param firstDate The first date or timestamp.\r\n * @param secondDate The second date or timestamp.\r\n * @returns The number of years between the two dates.\r\n */\r\n static yearsInBetween(firstDate: Date, secondDate: Date): number;\r\n static yearsInBetween(firstDate: number, secondDate: number): number;\r\n static yearsInBetween(firstDate: Date | number, secondDate: Date | number): number {\r\n const first = typeof firstDate === 'number' ? new Date(firstDate) : firstDate;\r\n const second = typeof secondDate === 'number' ? new Date(secondDate) : secondDate;\r\n return Math.abs(differenceInCalendarYears(first, second));\r\n }\r\n\r\n /**\r\n * Compares two dates or timestamps.\r\n * @param firstDate The first date or timestamp.\r\n * @param secondDate The second date or timestamp.\r\n * @returns `-1` if the first date is earlier, `1` if it is later, or `0` if the two dates are equal.\r\n */\r\n static compareDates(firstDate: Date, secondDate: Date): -1 | 0 | 1;\r\n static compareDates(firstDate: number, secondDate: number): -1 | 0 | 1;\r\n static compareDates(firstDate: Date | number, secondDate: Date | number): -1 | 0 | 1 {\r\n const first = typeof firstDate === 'number' ? new Date(firstDate) : firstDate;\r\n const second = typeof secondDate === 'number' ? new Date(secondDate) : secondDate;\r\n\r\n return compareAsc(first, second) as -1 | 0 | 1;\r\n }\r\n\r\n /**\r\n * Adds a duration (e.g., days, months, years) to a given date or timestamp.\r\n * @param date The base date or timestamp to which the duration will be added.\r\n * @param duration An object specifying the duration (e.g., `{ days: 1, months: 2 }`).\r\n * @returns A new Date object with the duration added.\r\n */\r\n static addDuration(date: Date, duration: Duration): Date;\r\n static addDuration(date: number, duration: Duration): Date;\r\n static addDuration(date: Date | number, duration: Duration): Date {\r\n const normalizedDate = typeof date === 'number' ? new Date(date) : date;\r\n return add(normalizedDate, duration);\r\n }\r\n\r\n /**\r\n * Determines whether a given year is a leap year.\r\n * @param year The year to check.\r\n * @returns `true` if the year is a leap year, otherwise `false`.\r\n */\r\n static isLeapYear(year: number): boolean {\r\n return isLeapYear(DateUtil.readDate(year.toString(), 'yyyy'));\r\n }\r\n\r\n\r\n /**\r\n * Converts a timestamp (in milliseconds) to a Date object.\r\n * @param milliseconds The timestamp in milliseconds since the Unix epoch.\r\n * @returns A Date object representing the provided timestamp.\r\n */\r\n static fromMillis(milliseconds: number): Date {\r\n return new Date(milliseconds);\r\n }\r\n\r\n /**\r\n * Converts a Date object to a timestamp (in milliseconds).\r\n * @param date The Date object to convert.\r\n * @returns The timestamp in milliseconds since the Unix epoch.\r\n */\r\n static toMillis(date: Date): number {\r\n return date.getTime();\r\n }\r\n}\r\n\r\n/**\r\n * Utility class for handling date operations in a fixed time zone.\r\n * Extends BaseDateUtil with default settings for the 'Asia/Kolkata' time zone.\r\n */\r\nexport class DateUtil extends BaseDateUtil {\r\n /** Default time zone used for all date operations. */\r\n static readonly TIMEZONE = 'Asia/Kolkata';\r\n /**\r\n * Gets the current date and time in the default time zone.\r\n * @returns Current date and time as a Date object.\r\n */\r\n static override now(): Date {\r\n return BaseDateUtil.now(this.TIMEZONE);\r\n }\r\n\r\n /**\r\n * Parses a date string into a Date object using the default time zone.\r\n * @param dateString The date string to parse.\r\n * @param dateFormat Optional format of the date string (default: ISO_8601_FORMAT).\r\n * @throws {IllegalArgumentException} If the date string is invalid.\r\n * @returns Parsed Date object.\r\n */\r\n static override readDate(dateString: string, dateFormat?: string): Date {\r\n return BaseDateUtil.readDate(dateString, dateFormat || DateUtil.ISO_8601_FORMAT, this.TIMEZONE);\r\n }\r\n\r\n /**\r\n * Formats a Date or timestamp into a string using the default time zone.\r\n * @param date The date or timestamp to format.\r\n * @param dateFormat Optional desired output format (default: ISO_8601_FORMAT).\r\n * @returns Formatted date string.\r\n */\r\n static override printDate(date: Date, dateFormat?: string): string;\r\n static override printDate(date: number, dateFormat?: string): string;\r\n static override printDate(date: Date | number, dateFormat = DateUtil.ISO_8601_FORMAT): string {\r\n const normalizedDate = typeof date === 'number' ? new Date(date) : date;\r\n return BaseDateUtil.printDate(normalizedDate, this.TIMEZONE, dateFormat);\r\n }\r\n\r\n /**\r\n * Gets the start of the day for a given date or timestamp using the default time zone.\r\n * @param date The date or timestamp to calculate from.\r\n * @returns A Date object representing the start of the day.\r\n */\r\n static override getStartOfDay(date: Date): Date;\r\n static override getStartOfDay(date: number): Date;\r\n static override getStartOfDay(date: Date | number): Date {\r\n const normalizedDate = typeof date === 'number' ? new Date(date) : date;\r\n return BaseDateUtil.getStartOfDay(normalizedDate, this.TIMEZONE);\r\n }\r\n\r\n /**\r\n * Gets the end of the day for a given date or timestamp using the default time zone.\r\n * @param date The date or timestamp to calculate from.\r\n * @returns A Date object representing the end of the day.\r\n */\r\n static override getEndOfDay(date: Date): Date;\r\n static override getEndOfDay(date: number): Date;\r\n static override getEndOfDay(date: Date | number): Date {\r\n const normalizedDate = typeof date === 'number' ? new Date(date) : date;\r\n return BaseDateUtil.getEndOfDay(normalizedDate, this.TIMEZONE);\r\n }\r\n}\r\n"],"names":[],"mappings":";;;;AAIA;;;AAGG;AACH,MAAM,YAAY,CAAA;aAEE,IAAe,CAAA,eAAA,GAAG,8BAA8B,CAAA;AAEhE;;;;AAIG;IACO,OAAO,GAAG,CAAC,QAAgB,EAAA;QACjC,OAAO,IAAI,MAAM,CAAC,IAAI,IAAI,EAAE,EAAE,QAAQ,CAAC;;AAG3C;;;AAGG;AACH,IAAA,OAAO,WAAW,GAAA;AACd,QAAA,OAAO,IAAI,CAAC,GAAG,EAAE;;AAGrB;;;;;;;AAOG;IACO,OAAO,QAAQ,CAAC,UAAkB,EAAE,UAAU,GAAG,IAAI,CAAC,eAAe,EAAE,QAAgB,EAAA;AAC7F,QAAA,MAAM,IAAI,GAAG,KAAK,CAAC,UAAU,EAAE,UAAU,EAAE,MAAM,CAAC,EAAE,CAAC,QAAQ,CAAC,CAAC;AAC/D,QAAA,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE;AAChB,YAAA,MAAM,IAAI,wBAAwB,CAAC,oCAAoC,CAAC;;AAE5E,QAAA,OAAO,IAAI;;IAYL,OAAO,SAAS,CAAC,IAAmB,EAAE,QAAgB,EAAE,UAAA,GAAqB,IAAI,CAAC,eAAe,EAAA;AACvG,QAAA,MAAM,cAAc,GAAG,OAAO,IAAI,KAAK,QAAQ,GAAG,IAAI,IAAI,CAAC,IAAI,CAAC,GAAG,IAAI;AACvE,QAAA,OAAO,MAAM,CAAC,IAAI,MAAM,CAAC,cAAc,EAAE,QAAQ,CAAC,EAAE,UAAU,CAAC;;AAWzD,IAAA,OAAO,aAAa,CAAC,IAAmB,EAAE,QAAgB,EAA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,CAAC;;;;;;"}
|
|
1
|
+
{"version":3,"file":"DateUtil.js","sources":["../../../../src/utils/date/DateUtil.ts"],"sourcesContent":["import { TZDate } from '@date-fns/tz';\r\nimport { add, compareAsc, differenceInCalendarDays, differenceInCalendarMonths, differenceInCalendarYears, differenceInYears, Duration, endOfDay, format, isLeapYear, isValid, parse, startOfDay } from 'date-fns';\r\nimport { IllegalArgumentException } from '../../exceptions';\r\n\r\n/**\r\n * Utility class for date and time operations. Provides methods for parsing,\r\n * formatting, and performing calculations with dates and times.\r\n */\r\nclass BaseDateUtil {\r\n\r\n static readonly ISO_8601_FORMAT = \"yyyy-MM-dd'T'HH:mm:ss.SSSxxx\"\r\n\r\n /**\r\n * Gets the current date and time in the specified time zone.\r\n * @param timeZone The IANA time zone identifier.\r\n * @returns Current date and time in the specified time zone.\r\n */\r\n protected static now(timeZone: string): Date {\r\n return new TZDate(new Date(), timeZone);\r\n }\r\n\r\n /**\r\n * Gets the current time in milliseconds since the Unix epoch.\r\n * @returns Current time in milliseconds.\r\n */\r\n static nowInMillis(): number {\r\n return Date.now();\r\n }\r\n\r\n /**\r\n * Parses a date string into a Date object.\r\n * @param dateString The date string to parse.\r\n * @param dateFormat The format of the date string (default: ISO_8601_FORMAT).\r\n * @param timeZone The IANA time zone identifier.\r\n * @throws {IllegalArgumentException} If the date string is invalid.\r\n * @returns Parsed Date object.\r\n */\r\n protected static readDate(dateString: string, dateFormat = this.ISO_8601_FORMAT, timeZone: string): Date {\r\n const date = parse(dateString, dateFormat, TZDate.tz(timeZone));\r\n if (!isValid(date)) {\r\n throw new IllegalArgumentException('Invalid date string or date format');\r\n }\r\n return date;\r\n }\r\n\r\n /**\r\n * Formats a Date or timestamp into a string.\r\n * @param date The date or timestamp to format.\r\n * @param timeZone The IANA time zone identifier.\r\n * @param dateFormat The desired output format (default: ISO_8601_FORMAT).\r\n * @returns Formatted date string.\r\n */\r\n protected static printDate(date: Date, timeZone: string, dateFormat?: string): string;\r\n protected static printDate(date: number, timeZone: string, dateFormat?: string): string;\r\n protected static printDate(date: Date | number, timeZone: string, dateFormat: string = this.ISO_8601_FORMAT): string {\r\n const normalizedDate = typeof date === 'number' ? new Date(date) : date;\r\n return format(new TZDate(normalizedDate, timeZone), dateFormat);\r\n }\r\n\r\n /**\r\n * Gets the start of the day for a given date or timestamp.\r\n * @param date The date or timestamp to calculate from.\r\n * @param timeZone The IANA time zone identifier.\r\n * @returns The start of the day as a Date object.\r\n */\r\n protected static getStartOfDay(date: Date, timeZone: string): Date;\r\n protected static getStartOfDay(date: number, timeZone: string): Date;\r\n protected static getStartOfDay(date: Date | number, timeZone: string): Date {\r\n const normalizedDate = typeof date === 'number' ? new Date(date) : date;\r\n return startOfDay(new TZDate(normalizedDate, timeZone));\r\n }\r\n\r\n /**\r\n * Gets the end of the day for a given date or timestamp.\r\n * @param date The date or timestamp to calculate from.\r\n * @param timeZone The IANA time zone identifier.\r\n * @returns The end of the day as a Date object.\r\n */\r\n protected static getEndOfDay(date: Date, timeZone: string): Date;\r\n protected static getEndOfDay(date: number, timeZone: string): Date;\r\n protected static getEndOfDay(date: Date | number, timeZone: string): Date {\r\n const normalizedDate = typeof date === 'number' ? new Date(date) : date;\r\n return endOfDay(new TZDate(normalizedDate, timeZone));\r\n }\r\n\r\n /**\r\n * Checks if a date string is valid according to the specified format.\r\n * @param dateString The date string to validate.\r\n * @param dateFormat Optional date format (default: ISO_8601_FORMAT).\r\n * @returns True if the date is valid, false otherwise.\r\n */\r\n static isValidDate(dateString: string, dateFormat?: string): boolean {\r\n try {\r\n DateUtil.readDate(dateString, dateFormat);\r\n return true;\r\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\r\n } catch (error) {\r\n return false;\r\n }\r\n }\r\n\r\n /**\r\n * Calculates the absolute number of days between two dates or timestamps.\r\n * @param firstDate The first date or timestamp.\r\n * @param secondDate The second date or timestamp.\r\n * @returns Number of days between the two dates.\r\n */\r\n static daysInBetween(firstDate: Date, secondDate: Date): number;\r\n static daysInBetween(firstDate: number, secondDate: number): number;\r\n static daysInBetween(firstDate: Date | number, secondDate: Date | number): number {\r\n const first = typeof firstDate === 'number' ? new Date(firstDate) : firstDate;\r\n const second = typeof secondDate === 'number' ? new Date(secondDate) : secondDate;\r\n return Math.abs(differenceInCalendarDays(first, second));\r\n }\r\n\r\n /**\r\n * Calculates the absolute number of months between two dates or timestamps.\r\n * @param firstDate The first date or timestamp.\r\n * @param secondDate The second date or timestamp.\r\n * @returns The number of months between the two dates.\r\n */\r\n static monthsInBetween(firstDate: Date, secondDate: Date): number;\r\n static monthsInBetween(firstDate: number, secondDate: number): number;\r\n static monthsInBetween(firstDate: Date | number, secondDate: Date | number): number {\r\n const first = typeof firstDate === 'number' ? new Date(firstDate) : firstDate;\r\n const second = typeof secondDate === 'number' ? new Date(secondDate) : secondDate;\r\n return Math.abs(differenceInCalendarMonths(first, second));\r\n }\r\n\r\n /**\r\n * Calculates the absolute number of years between two dates or timestamps.\r\n * @param firstDate The first date or timestamp.\r\n * @param secondDate The second date or timestamp.\r\n * @returns The number of years between the two dates.\r\n */\r\n static anniversaryYearsInBetween(firstDate: Date, secondDate: Date): number;\r\n static anniversaryYearsInBetween(firstDate: number, secondDate: number): number;\r\n static anniversaryYearsInBetween(firstDate: Date | number, secondDate: Date | number): number {\r\n const first = typeof firstDate === 'number' ? new Date(firstDate) : firstDate;\r\n const second = typeof secondDate === 'number' ? new Date(secondDate) : secondDate;\r\n return Math.abs(differenceInYears(first, second));\r\n }\r\n\r\n /**\r\n * Calculates the absolute number of years between two dates or timestamps.\r\n * @param firstDate The first date or timestamp.\r\n * @param secondDate The second date or timestamp.\r\n * @returns The number of years between the two dates.\r\n */\r\n static yearsInBetween(firstDate: Date, secondDate: Date): number;\r\n static yearsInBetween(firstDate: number, secondDate: number): number;\r\n static yearsInBetween(firstDate: Date | number, secondDate: Date | number): number {\r\n const first = typeof firstDate === 'number' ? new Date(firstDate) : firstDate;\r\n const second = typeof secondDate === 'number' ? new Date(secondDate) : secondDate;\r\n return Math.abs(differenceInCalendarYears(first, second));\r\n }\r\n\r\n /**\r\n * Compares two dates or timestamps.\r\n * @param firstDate The first date or timestamp.\r\n * @param secondDate The second date or timestamp.\r\n * @returns `-1` if the first date is earlier, `1` if it is later, or `0` if the two dates are equal.\r\n */\r\n static compareDates(firstDate: Date, secondDate: Date): -1 | 0 | 1;\r\n static compareDates(firstDate: number, secondDate: number): -1 | 0 | 1;\r\n static compareDates(firstDate: Date | number, secondDate: Date | number): -1 | 0 | 1 {\r\n const first = typeof firstDate === 'number' ? new Date(firstDate) : firstDate;\r\n const second = typeof secondDate === 'number' ? new Date(secondDate) : secondDate;\r\n\r\n return compareAsc(first, second) as -1 | 0 | 1;\r\n }\r\n\r\n /**\r\n * Adds a duration (e.g., days, months, years) to a given date or timestamp.\r\n * @param date The base date or timestamp to which the duration will be added.\r\n * @param duration An object specifying the duration (e.g., `{ days: 1, months: 2 }`).\r\n * @returns A new Date object with the duration added.\r\n */\r\n static addDuration(date: Date, duration: Duration): Date;\r\n static addDuration(date: number, duration: Duration): Date;\r\n static addDuration(date: Date | number, duration: Duration): Date {\r\n const normalizedDate = typeof date === 'number' ? new Date(date) : date;\r\n return add(normalizedDate, duration);\r\n }\r\n\r\n /**\r\n * Determines whether a given year is a leap year.\r\n * @param year The year to check.\r\n * @returns `true` if the year is a leap year, otherwise `false`.\r\n */\r\n static isLeapYear(year: number): boolean {\r\n return isLeapYear(DateUtil.readDate(year.toString(), 'yyyy'));\r\n }\r\n\r\n\r\n /**\r\n * Converts a timestamp (in milliseconds) to a Date object.\r\n * @param milliseconds The timestamp in milliseconds since the Unix epoch.\r\n * @returns A Date object representing the provided timestamp.\r\n */\r\n static fromMillis(milliseconds: number): Date {\r\n return new Date(milliseconds);\r\n }\r\n\r\n /**\r\n * Converts a Date object to a timestamp (in milliseconds).\r\n * @param date The Date object to convert.\r\n * @returns The timestamp in milliseconds since the Unix epoch.\r\n */\r\n static toMillis(date: Date): number {\r\n return date.getTime();\r\n }\r\n}\r\n\r\n/**\r\n * Utility class for handling date operations in a fixed time zone.\r\n * Extends BaseDateUtil with default settings for the 'Asia/Kolkata' time zone.\r\n */\r\nexport class DateUtil extends BaseDateUtil {\r\n /** Default time zone used for all date operations. */\r\n static readonly TIMEZONE = 'Asia/Kolkata';\r\n /**\r\n * Gets the current date and time in the default time zone.\r\n * @returns Current date and time as a Date object.\r\n */\r\n static override now(): Date {\r\n return BaseDateUtil.now(this.TIMEZONE);\r\n }\r\n\r\n /**\r\n * Parses a date string into a Date object using the default time zone.\r\n * @param dateString The date string to parse.\r\n * @param dateFormat Optional format of the date string (default: ISO_8601_FORMAT).\r\n * @throws {IllegalArgumentException} If the date string is invalid.\r\n * @returns Parsed Date object.\r\n */\r\n static override readDate(dateString: string, dateFormat?: string): Date {\r\n return BaseDateUtil.readDate(dateString, dateFormat || DateUtil.ISO_8601_FORMAT, this.TIMEZONE);\r\n }\r\n\r\n /**\r\n * Formats a Date or timestamp into a string using the default time zone.\r\n * @param date The date or timestamp to format.\r\n * @param dateFormat Optional desired output format (default: ISO_8601_FORMAT).\r\n * @returns Formatted date string.\r\n */\r\n static override printDate(date: Date, dateFormat?: string): string;\r\n static override printDate(date: number, dateFormat?: string): string;\r\n static override printDate(date: Date | number, dateFormat = DateUtil.ISO_8601_FORMAT): string {\r\n const normalizedDate = typeof date === 'number' ? new Date(date) : date;\r\n return BaseDateUtil.printDate(normalizedDate, this.TIMEZONE, dateFormat);\r\n }\r\n\r\n /**\r\n * Gets the start of the day for a given date or timestamp using the default time zone.\r\n * @param date The date or timestamp to calculate from.\r\n * @returns A Date object representing the start of the day.\r\n */\r\n static override getStartOfDay(date: Date): Date;\r\n static override getStartOfDay(date: number): Date;\r\n static override getStartOfDay(date: Date | number): Date {\r\n const normalizedDate = typeof date === 'number' ? new Date(date) : date;\r\n return BaseDateUtil.getStartOfDay(normalizedDate, this.TIMEZONE);\r\n }\r\n\r\n /**\r\n * Gets the end of the day for a given date or timestamp using the default time zone.\r\n * @param date The date or timestamp to calculate from.\r\n * @returns A Date object representing the end of the day.\r\n */\r\n static override getEndOfDay(date: Date): Date;\r\n static override getEndOfDay(date: number): Date;\r\n static override getEndOfDay(date: Date | number): Date {\r\n const normalizedDate = typeof date === 'number' ? new Date(date) : date;\r\n return BaseDateUtil.getEndOfDay(normalizedDate, this.TIMEZONE);\r\n }\r\n}\r\n"],"names":[],"mappings":";;;;AAIA;;;AAGG;AACH,MAAM,YAAY,CAAA;aAEE,IAAe,CAAA,eAAA,GAAG,8BAA8B,CAAA;AAEhE;;;;AAIG;IACO,OAAO,GAAG,CAAC,QAAgB,EAAA;QACjC,OAAO,IAAI,MAAM,CAAC,IAAI,IAAI,EAAE,EAAE,QAAQ,CAAC;;AAG3C;;;AAGG;AACH,IAAA,OAAO,WAAW,GAAA;AACd,QAAA,OAAO,IAAI,CAAC,GAAG,EAAE;;AAGrB;;;;;;;AAOG;IACO,OAAO,QAAQ,CAAC,UAAkB,EAAE,UAAU,GAAG,IAAI,CAAC,eAAe,EAAE,QAAgB,EAAA;AAC7F,QAAA,MAAM,IAAI,GAAG,KAAK,CAAC,UAAU,EAAE,UAAU,EAAE,MAAM,CAAC,EAAE,CAAC,QAAQ,CAAC,CAAC;AAC/D,QAAA,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE;AAChB,YAAA,MAAM,IAAI,wBAAwB,CAAC,oCAAoC,CAAC;;AAE5E,QAAA,OAAO,IAAI;;IAYL,OAAO,SAAS,CAAC,IAAmB,EAAE,QAAgB,EAAE,UAAA,GAAqB,IAAI,CAAC,eAAe,EAAA;AACvG,QAAA,MAAM,cAAc,GAAG,OAAO,IAAI,KAAK,QAAQ,GAAG,IAAI,IAAI,CAAC,IAAI,CAAC,GAAG,IAAI;AACvE,QAAA,OAAO,MAAM,CAAC,IAAI,MAAM,CAAC,cAAc,EAAE,QAAQ,CAAC,EAAE,UAAU,CAAC;;AAWzD,IAAA,OAAO,aAAa,CAAC,IAAmB,EAAE,QAAgB,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,IAAI,KAAK,QAAQ,GAAG,IAAI,IAAI,CAAC,IAAI,CAAC,GAAG,IAAI;QACvE,OAAO,YAAY,CAAC,aAAa,CAAC,cAAc,EAAE,IAAI,CAAC,QAAQ,CAAC;;IAUpE,OAAgB,WAAW,CAAC,IAAmB,EAAA;AAC3C,QAAA,MAAM,cAAc,GAAG,OAAO,IAAI,KAAK,QAAQ,GAAG,IAAI,IAAI,CAAC,IAAI,CAAC,GAAG,IAAI;QACvE,OAAO,YAAY,CAAC,WAAW,CAAC,cAAc,EAAE,IAAI,CAAC,QAAQ,CAAC;;;;;;"}
|
|
@@ -74,6 +74,14 @@ declare class BaseDateUtil {
|
|
|
74
74
|
*/
|
|
75
75
|
static monthsInBetween(firstDate: Date, secondDate: Date): number;
|
|
76
76
|
static monthsInBetween(firstDate: number, secondDate: number): number;
|
|
77
|
+
/**
|
|
78
|
+
* Calculates the absolute number of years between two dates or timestamps.
|
|
79
|
+
* @param firstDate The first date or timestamp.
|
|
80
|
+
* @param secondDate The second date or timestamp.
|
|
81
|
+
* @returns The number of years between the two dates.
|
|
82
|
+
*/
|
|
83
|
+
static anniversaryYearsInBetween(firstDate: Date, secondDate: Date): number;
|
|
84
|
+
static anniversaryYearsInBetween(firstDate: number, secondDate: number): number;
|
|
77
85
|
/**
|
|
78
86
|
* Calculates the absolute number of years between two dates or timestamps.
|
|
79
87
|
* @param firstDate The first date or timestamp.
|