Date
The Date module provides a comprehensive set of functions for manipulating, formatting, and validating dates in JavaScript. With these functions, you can easily perform common date operations such as timezone adjustments, elapsed time calculations, special date checks, and much more.
- Date and time manipulation
- Date formatting and validation
- Elapsed time calculations
- Special date checks
- Timezone adjustments
Overview
adjustDate(date: Date, adjustment: Partial<{ years: number; months: number; days: number; hours: number; minutes: number; seconds: number; milliseconds: number; }>): Date
Adjusts a date by adding or subtracting years, months, days, hours, minutes, seconds, or milliseconds.
adjustTimezone(date: Date, targetTimezone: number): Date
Adjusts a date to a target timezone, returning a new Date instance.
convertDateTo<T extends 'string' | 'number' | 'date'>(date: Date, type: T): string | number | Date
Converts a date to a specific output type: string, number, or Date object.
convertTimeUnit(value: number, from: string, to: string): number
Converts values between different time units.
elapsedTime(start: Date, end: Date, unit?: string): number
Calculates the elapsed time between two dates and returns the result in the desired unit.
formatDate(date: Date, pattern?: string): string
Formats a date according to a specified string pattern.
formatDuration(ms: number, options?: { compact?: boolean }): string
Converts a duration in milliseconds to a human-readable string.
formatTime(date: Date, pattern?: string): string
Formats a time (Date) to a string in the specified format.
isBetween(date: Date, start: Date, end: Date, inclusive?: boolean): boolean
Checks if a date is between two other dates.
isLeapYear(yearOrDate: number | Date): boolean
Checks if a year or date corresponds to a leap year.
isSameDay(date1: Date, date2: Date): boolean
Checks if two dates represent the same day.
isToday(date: Date): boolean
Checks if a date corresponds to today.
isTomorrow(date: Date): boolean
Checks if a date corresponds to tomorrow.
isValidDate(value: any): boolean
Checks if a value is a valid JavaScript date.
isValidDateString(value: string): boolean
Checks if a string represents a valid date.
isWeekday(date: Date): boolean
Checks if a date is a weekday (Monday to Friday).
isWeekend(date: Date): boolean
Checks if a date is a weekend.
isYesterday(date: Date): boolean
Checks if a date corresponds to the day before today.
parseDate(date: Date | string | number): Date
Converts a value into a valid Date instance.
stripTime(date: Date): Date
Removes the time information from a date, returning only the year, month, and day.
toDate(value: Date | string | number): Date
Converts a value into a Date instance.