Skip to content

Instantly share code, notes, and snippets.

@avaleriani
Created August 3, 2018 20:16
Show Gist options
  • Save avaleriani/62edb971ba700e8b7f5474fa8ed09428 to your computer and use it in GitHub Desktop.
Save avaleriani/62edb971ba700e8b7f5474fa8ed09428 to your computer and use it in GitHub Desktop.
//31.01.2000
// startDateOfContract = parameter
//customerAgeWhenContractEnd = parameter
//return end date of the contract = 1 day before the start date at the moment that the customer has the age he picked.
const calculateEndDateOfContract = (customerBirthDate, startDateOfContract, customerAgeWhenContractEnd) => {
customerBirthDate = splitDate(customerBirthDate);
startDateOfContract = splitDate(startDateOfContract);
let endContractDate = new Date(customerBirthDate.year + customerAgeWhenContractEnd, startDateOfContract.month - 1, startDateOfContract.day - 1);
let endContractDay = endContractDate.getDate();
let endContractMonth = endContractDate.getMonth() + 1;
let endContractYear = endContractDate.getFullYear();
if(customerBirthDate.month > endContractMonth){
endContractYear++;
}else if(endContractMonth === customerBirthDate.month && endContractDate.getDate() < customerBirthDate.day){
endContractYear++;
}
if(endContractMonth < 10){
endContractMonth = `0${endContractMonth}`
}
if(endContractDay < 10){
endContractDay = `0${endContractDay}`
}
return `${endContractDay}.${endContractMonth}.${endContractYear}`
};
const splitDate = (dateStr) =>{
const splitDate = dateStr.split(".")
return {
day: parseInt(splitDate[0], 10),
month:parseInt(splitDate[1], 10),
year: parseInt(splitDate[2], 10)
}
}
console.log(calculateEndDateOfContract("15.09.2000", "15.09.2018", 65));
console.log(calculateEndDateOfContract("16.09.2000", "15.09.2018", 65));
console.log(calculateEndDateOfContract("14.09.2000", "15.09.2018", 65));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment