Skip to content

Instantly share code, notes, and snippets.

@jmealo
Last active September 13, 2016 21:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmealo/8586b6f954e289b78a7d9e8fc5424b28 to your computer and use it in GitHub Desktop.
Save jmealo/8586b6f954e289b78a7d9e8fc5424b28 to your computer and use it in GitHub Desktop.
Functions for excluding specific dates and weekends from time calculations
const MS_IN_DAY = 86400000;
// Pretend this is the start/end time for a phase
var startDate = new Date("01/24/1989 08:30:23"),
endDate = new Date("01/31/1989 16:32:23"),
// This will be provided by the API -- extracted from a Google Calendar/iCal feed
daysOff = [
new Date("01/26/1989"),
new Date("01/27/1989")
];
// Resets time to 00:00:00 on a date
function stripTime(date) {
return new Date(date.toDateString());
}
// Returns true for weekends (Sat/Sun)
function isWeekend(date) {
var day = date.getDay();
return day === 0 || day === 6;
}
// Returns true for Monday-Friday
function isWeekDay(date) {
return !isWeekend(date);
}
// Returns an array of date objects for every day between startDate and endDate (inclusive)
function getDatesBetween(startDate, endDate) {
var startDate = stripTime(startDate),
endDate = stripTime(endDate),
date = startDate,
dates = [startDate];
while (date < endDate) {
date = new Date(date.getTime() + MS_IN_DAY);
dates.push(date);
}
return dates;
}
// Strip the time component of a date object
console.log(
'Stripping the time from a date object:\n',
stripTime(startDate)
);
// Get an array of date objects for every day between two dates
console.log(getDatesBetween(startDate, endDate));
// Strip weekends
console.log(
'Exclude weekends:\n',
getDatesBetween(startDate, endDate)
.filter(date => !isWeekend(date))
);
// Strip days off from school
console.log(
'Exclude 1/26 and 1/27:\n',
getDatesBetween(startDate, endDate)
.filter(date => daysOff.indexOf(date) === -1)
);
// Strip days off from school and weekends
console.log('Exclude weekends and 1/26 and 1/27:\n',
getDatesBetween(startDate, endDate)
.filter(date => daysOff.indexOf(date) === -1)
.filter(date => !isWeekend(date))
);
// Returns an integer for
function sectionCodeToGrade(code) {
var matches = code.match(/(\d+|K)-\d+/);
if (!matches) {
throw new Error('Unable to extract grade from section code: ' + code);
}
return matches[1] === 'K' ? 0 : parseInt(matches[1], 10);
}
1989-01-24T05:00:00.000Z
[ 1989-01-24T05:00:00.000Z,
1989-01-25T05:00:00.000Z,
1989-01-26T05:00:00.000Z,
1989-01-27T05:00:00.000Z,
1989-01-28T05:00:00.000Z,
1989-01-29T05:00:00.000Z,
1989-01-30T05:00:00.000Z,
1989-01-31T05:00:00.000Z ]
Exclude weekends:
[ 1989-01-24T05:00:00.000Z,
1989-01-25T05:00:00.000Z,
1989-01-26T05:00:00.000Z,
1989-01-27T05:00:00.000Z,
1989-01-30T05:00:00.000Z,
1989-01-31T05:00:00.000Z ]
Exclude 1/26 and 1/27:
[ 1989-01-24T05:00:00.000Z,
1989-01-25T05:00:00.000Z,
1989-01-26T05:00:00.000Z,
1989-01-27T05:00:00.000Z,
1989-01-28T05:00:00.000Z,
1989-01-29T05:00:00.000Z,
1989-01-30T05:00:00.000Z,
1989-01-31T05:00:00.000Z ]
Exclude weekends and 1/26 and 1/27:
[ 1989-01-24T05:00:00.000Z,
1989-01-25T05:00:00.000Z,
1989-01-26T05:00:00.000Z,
1989-01-27T05:00:00.000Z,
1989-01-30T05:00:00.000Z,
1989-01-31T05:00:00.000Z ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment