Created
July 5, 2018 14:16
-
-
Save mx781/065d779f9875de871ca99882e5e6f561 to your computer and use it in GitHub Desktop.
weekdaysBetween - get number of weekdays occurring between two moment objects (inclusively)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { weekdaysBetween } from './weekdaysBetween'; | |
describe('Report helpers', () => { | |
describe('weekdaysBetween', () => { | |
it('Returns 0 for a Tuesday on a 1-day Monday range', () => { | |
const tuesdayCount = weekdaysBetween(moment('2018-07-02'), moment('2018-07-02'), 2); | |
expect(tuesdayCount).toBe(0); | |
}); | |
it('Returns 1 for a Monday on a 1-day Monday range', () => { | |
const mondayCount = weekdaysBetween(moment('2018-07-02'), moment('2018-07-02'), 1); | |
expect(mondayCount).toBe(1); | |
}); | |
const isoWeekDays = Array(7).fill(0).map((_, i) => i + 1); | |
it('Works for a four-day range', () => { | |
const counts = {}; | |
isoWeekDays.forEach(isoWeekday => { | |
counts[isoWeekday] = weekdaysBetween(moment('2018-04-06'), moment('2018-04-09'), isoWeekday); | |
}); | |
expect(counts['1']).toBe(1); | |
expect(counts['2']).toBe(0); | |
expect(counts['3']).toBe(0); | |
expect(counts['4']).toBe(0); | |
expect(counts['5']).toBe(1); | |
expect(counts['6']).toBe(1); | |
expect(counts['7']).toBe(1); | |
}); | |
it('Returns 1 for each day on a weeklong range', () => { | |
isoWeekDays.forEach(isoWeekday => { | |
const weekdayCount = weekdaysBetween( | |
moment('2018-01-01'), | |
moment('2018-01-07'), | |
isoWeekday, | |
); | |
expect(weekdayCount).toBe(1); | |
}); | |
}); | |
it('Returns 2 for each day on a two week long range', () => { | |
isoWeekDays.forEach(isoWeekday => { | |
const weekdayCount = weekdaysBetween( | |
moment('2018-05-07'), | |
moment('2018-05-20'), | |
isoWeekday, | |
); | |
expect(weekdayCount).toBe(2); | |
}); | |
}); | |
it('Returns 0 for all days for an invalid range', () => { | |
isoWeekDays.forEach(isoWeekday => { | |
const weekdayCount = weekdaysBetween( | |
moment('2019-12-12'), | |
moment('2009-01-01'), | |
isoWeekday, | |
); | |
expect(weekdayCount).toBe(0); | |
}); | |
}); | |
it('Returns 52 for each day for a 52-week long period', () => { | |
isoWeekDays.forEach(isoWeekday => { | |
const weekdayCount = weekdaysBetween( | |
moment('2018-01-01'), | |
moment('2018-12-30'), | |
isoWeekday, | |
); | |
expect(weekdayCount).toBe(52); | |
}); | |
}); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Adapted from https://stackoverflow.com/a/29091126/5814943 | |
export const weekdaysBetween = (startDate: moment.Moment, endDate: moment.Moment, isoWeekday: number) => { | |
if (startDate.isSame(endDate, 'day')) { | |
return startDate.isoWeekday() === isoWeekday | |
? 1 | |
: 0; | |
} | |
const daysToAdd = ((7 + isoWeekday) - startDate.isoWeekday()) % 7; | |
const nextOccurence = startDate.clone().add(daysToAdd, 'days'); | |
if (nextOccurence.isAfter(endDate)) { | |
return 0; | |
} | |
const weeksBetween = endDate.diff(nextOccurence, 'weeks'); | |
return weeksBetween + 1; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment