Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save codeeshop-oc/638ae33a6d081c4f52640b832721bd03 to your computer and use it in GitHub Desktop.
Save codeeshop-oc/638ae33a6d081c4f52640b832721bd03 to your computer and use it in GitHub Desktop.
Set Start and End Date Based on Number of days
let getDateInFormat = function (dated) {
let dd = String(dated.getDate()).padStart(2, '0');
let mm = String(dated.getMonth() + 1).padStart(2, '0'); //January is 0!
let yyyy = dated.getFullYear();
return dated = yyyy + '-' + mm + '-' + dd;
}
let addDays = function (date, addDays) {
return date.setDate(date.getDate() + parseInt(addDays));
}
let setDates = function () {
if(new Date($('#startDate').val()) == 'Invalid Date') {
let startdate = getDateInFormat(new Date())
$('#startDate').val(startdate)
let enddate = new Date(addDays(new Date(), $('#no_of_days').val()))
$('#endDate').val(getDateInFormat(enddate))
} else if($('#startDate').val()) {
let enddate = new Date(addDays(new Date($('#startDate').val()), $('#no_of_days').val()))
$('#endDate').val(getDateInFormat(enddate))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment