@pj1401/date-utils
v1.2.2
Published
Date utilities
Readme
date-utils
This is a collection of utility methods for the Date object.
Install
Using npm:
npm i @pj1401/date-utilsUsage
Import:
import { DateUtil } from "@pj1401/date-utils";
const dateUtil = new DateUtil()Methods:
- countDays
- getWeekNumber
- getOrdinalDate
- getDaysBetween
- getDatesBetween
- getQuarter
- isBetween
- startOfWeek
- endOfWeek
- getDaysUntilWeekend
- countWorkingDays
- addDays
- removeDays
countDays
countDays(date: Date): number
Returns the number of days until a date. If the date is in the past, the number of days passed is returned (negative).
const nextWeek = new Date()
nextWeek.setDate(nextWeek.getDate() + 7)
dateUtil.countDays(nextWeek) // returns 7
dateUtil.countDays(new Date('2025-09-30')) // returns a negative numbergetWeekNumber
getWeekNumber(date: Date): number
Returns the number of the week that the date is in.
dateUtil.getWeekNumber(new Date('2025-09-17')) // returns 38getOrdinalDate
getOrdinalDate(date: Date): number
Returns the number representing the day of the year.
dateUtil.getOrdinalDate(new Date('2025-09-30')) // returns 273getDaysBetween
getDaysBetween(date1: Date, date2: Date): number
Returns the number of days between two dates.
dateUtil.getDaysBetween(new Date('2025-09-15'), new Date('2025-09-22')) // returns 7getDatesBetween
getDatesBetween(startDate: Date, endDate: Date): Date[]
Get an array of dates between two dates, inclusive.
dateUtil.getDatesBetween(new Date('2025-10-25'), new Date('2025-11-02'))getQuarter
getQuarter(date: Date): number
Returns the quarter of the year the date is in.
dateUtil.getQuarter(new Date('2025-10-01')) // returns 4isBetween
isBetween(date: Date, startDate: Date, endDate: Date): boolean
Check if a date is in the interval [startDate, endDate].
dateUtil.isBetween(new Date('2025-09-30'), new Date('2025-09-29'), new Date('2025-10-01')) // truestartOfWeek
startOfWeek(date: Date): Date
Returns the start (Monday) of the week the date is in.
dateUtil.startOfWeek(new Date('2025-09-30')) // returns a Date object set to 2025-09-29endOfWeek
endOfWeek(date: Date): Date
Returns the end (Sunday) of the week the date is in.
dateUtil.endOfWeek(new Date('2025-09-30')) // returns a Date object set to 2025-10-05getDaysUntilWeekend
getDaysUntilWeekend(date: Date): number
Get the number of days until the weekend (Saturday). If the date argument is a Sunday, it counts the days until the next weekend. If no argument is passed the current date will be used.
dateUtil.getDaysUntilWeekend(new Date('2025-11-05')) // returns 3countWorkingDays
countWorkingDays(startDate: Date, endDate: Date): number
Get the number of working days between two dates.
dateUtil.countWorkingDays(new Date('2025-10-20'), new Date('2025-11-01')) // returns 10addDays
addDays(date: Date, numberOfDays: number): Date
Add days to the date. The returned date is a different instance from the date argument.
dateUtil.addDays(new Date('2025-01-30'), 5) // returns a Date object set to 2025-02-04removeDays
removeDays(date: Date, numberOfDays: number): Date
Remove days from the date. The returned date is a different instance from the date argument.
dateUtil.addDays(new Date('2025-01-03'), 10) // returns a Date object set to 2024-12-24