Skip to content

Instantly share code, notes, and snippets.

@MarJaysonSanAgustin
Created October 1, 2017 10:57
Show Gist options
  • Save MarJaysonSanAgustin/5531ccc2ac6ce62719b897723995b996 to your computer and use it in GitHub Desktop.
Save MarJaysonSanAgustin/5531ccc2ac6ce62719b897723995b996 to your computer and use it in GitHub Desktop.
Get the dates between date range in typescript
// Main class
export class AppComponent {
_dateFunc: dateFunc = new dateFunc()
// then somewhere in your main component code add this
const dat = this._dateFunc.betweenDate(new Date('2017-01-11'), new Date('2017-02-13'));
console.log(dat)
}
// date function class
class dateFunc {
isDate(dateArg) {
var t = (dateArg instanceof Date) ? dateArg : (new Date(dateArg));
return !isNaN(t.valueOf());
}
isValidRange(minDate, maxDate) {
return (new Date(minDate) <= new Date(maxDate));
}
betweenDate(startDt, endDt) {
var error = ((this.isDate(endDt)) && (this.isDate(startDt)) && this.isValidRange(startDt, endDt)) ? false : true;
var between = [];
if (error) console.log('error occured!!!... Please Enter Valid Dates');
else {
var currentDate = new Date(startDt),
end = new Date(endDt);
while (currentDate <= end) {
between.push(new Date(currentDate));
currentDate.setDate(currentDate.getDate() + 1);
}
}
return between;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment