Skip to content

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

typescript
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

typescript
adjustTimezone(date: Date, targetTimezone: number): Date

Adjusts a date to a target timezone, returning a new Date instance.

convertDateTo

typescript
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

typescript
convertTimeUnit(value: number, from: string, to: string): number

Converts values between different time units.

elapsedTime

typescript
elapsedTime(start: Date, end: Date, unit?: string): number

Calculates the elapsed time between two dates and returns the result in the desired unit.

formatDate

typescript
formatDate(date: Date, pattern?: string): string

Formats a date according to a specified string pattern.

formatDuration

typescript
formatDuration(ms: number, options?: { compact?: boolean }): string

Converts a duration in milliseconds to a human-readable string.

formatTime

typescript
formatTime(date: Date, pattern?: string): string

Formats a time (Date) to a string in the specified format.

isBetween

typescript
isBetween(date: Date, start: Date, end: Date, inclusive?: boolean): boolean

Checks if a date is between two other dates.

isLeapYear

typescript
isLeapYear(yearOrDate: number | Date): boolean

Checks if a year or date corresponds to a leap year.

isSameDay

typescript
isSameDay(date1: Date, date2: Date): boolean

Checks if two dates represent the same day.

isToday

typescript
isToday(date: Date): boolean

Checks if a date corresponds to today.

isTomorrow

typescript
isTomorrow(date: Date): boolean

Checks if a date corresponds to tomorrow.

isValidDate

typescript
isValidDate(value: any): boolean

Checks if a value is a valid JavaScript date.

isValidDateString

typescript
isValidDateString(value: string): boolean

Checks if a string represents a valid date.

isWeekday

typescript
isWeekday(date: Date): boolean

Checks if a date is a weekday (Monday to Friday).

isWeekend

typescript
isWeekend(date: Date): boolean

Checks if a date is a weekend.

isYesterday

typescript
isYesterday(date: Date): boolean

Checks if a date corresponds to the day before today.

parseDate

typescript
parseDate(date: Date | string | number): Date

Converts a value into a valid Date instance.

stripTime

typescript
stripTime(date: Date): Date

Removes the time information from a date, returning only the year, month, and day.

toDate

typescript
toDate(value: Date | string | number): Date

Converts a value into a Date instance.

Released under the MIT License.