Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //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));
Add Comment
Please, Sign In to add comment