Skip to content

Instantly share code, notes, and snippets.

@mmgj
Last active June 8, 2018 20:20
Show Gist options
  • Save mmgj/6b88ae4c37e63f38a660ca6452a64bab to your computer and use it in GitHub Desktop.
Save mmgj/6b88ae4c37e63f38a660ca6452a64bab to your computer and use it in GitHub Desktop.
Simple date operations
export function isDateInRange(checkPointIn, startPointIn, endPointIn) {
if ([checkPointIn, startPointIn, endPointIn].some((point) => point === undefined)) {
console.warn('Function expects three parameters; checkPoint: Date, startPoint: Date, endPoint: Date');
return;
}
const checkPoint = new Date(checkPointIn).getTime();
const startPoint = new Date(startPointIn).getTime();
const endPoint = new Date(endPointIn).getTime();
return checkPoint > startPoint && checkPoint < endPoint;
}
export function isDateBeforeDate(firstDateIn, secondDateIn) {
if ([firstDateIn, secondDateIn].some((point) => point === undefined)) {
console.warn('Function expects two parameters; firstDate: Date, secondDate: Date');
return;
}
const firstDate = new Date(firstDateIn).getTime();
const secondDate = new Date(secondDateIn).getTime();
return firstDate < secondDate;
}
export function getDatesFromStartAndStep(startPointIn, stepSizeInMinsIn) {
if ([startPointIn, stepSizeInMinsIn].some((point) => point === undefined)) {
console.warn('Function expects two parameters; startPoint: Date, stepSizeInMins: int');
return;
}
const startPoint = new Date(startPointIn);
const endPoint = new Date(startPoint.getTime() + (stepSizeInMinsIn * 60 * 1000));
return [startPoint, endPoint];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment