-
Notifications
You must be signed in to change notification settings - Fork 0
/
addBusinessDays.ts
45 lines (42 loc) · 1.28 KB
/
addBusinessDays.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { addDays } from './addDays'
import { tzLocal } from './LocalTimezone'
import { Timezone } from './Timezone'
import { Calendar } from './Calendar'
import { calWeekends } from './WeekendCalendar'
/**
* Add business days to a date (or subtract if negative).
*
* If a calendar is not specified the {@linkcode calWeekends} calendar is used.
*
* ```js
* import { addBusinessDays } from '@jetblack/date'
*
* // Fri 7 Jan 2000
* const date = addBusinessDays(new Date('2000-01-07T00:00:00Z'), 1)
* console.log(date.getTime() === new Date('2000-01-10T00:00:00Z').getTime())
* ```
*
* @category Arithmetic
*
* @param date The start date.
* @param count The number of days to add (or subtract if negative).
* @param cal The calendar to use to identify dates which are holidays. Defaults to the {@linkcode calWeekends} calendar.
* @param tz An optional timezone. Defaults to the local timezone.
* @returns A new date adjusted by business days.
*/
export function addBusinessDays(
date: Date,
count: number,
cal: Calendar = calWeekends,
tz: Timezone = tzLocal
): Date {
const sign = count > 0 ? 1 : -1
while (count !== 0) {
date = addDays(date, sign, tz)
count -= sign
while (cal.isHoliday(date, tz)) {
date = addDays(date, sign, tz)
}
}
return date
}