thorpedosg

f16G083R

Aug 6th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. //31.01.2000
  2.  
  3. // startDateOfContract = parameter
  4. //customerAgeWhenContractEnd = parameter
  5. //return end date of the contract = 1 day before the start date at the moment that the customer has the age he picked.
  6.  
  7. const calculateEndDateOfContract = (customerBirthDate, startDateOfContract, customerAgeWhenContractEnd) => {
  8. customerBirthDate = splitDate(customerBirthDate);
  9. startDateOfContract = splitDate(startDateOfContract);
  10. let endContractDate = new Date(customerBirthDate.year + customerAgeWhenContractEnd, startDateOfContract.month - 1, startDateOfContract.day - 1);
  11. let endContractDay = endContractDate.getDate();
  12. let endContractMonth = endContractDate.getMonth() + 1;
  13. let endContractYear = endContractDate.getFullYear();
  14.  
  15. if(customerBirthDate.month > endContractMonth){
  16. endContractYear++;
  17. }else if(endContractMonth === customerBirthDate.month && endContractDate.getDate() < customerBirthDate.day){
  18. endContractYear++;
  19. }
  20.  
  21. if(endContractMonth < 10){
  22. endContractMonth = `0${endContractMonth}`
  23. }
  24. if(endContractDay < 10){
  25. endContractDay = `0${endContractDay}`
  26. }
  27.  
  28. return `${endContractDay}.${endContractMonth}.${endContractYear}`
  29. };
  30.  
  31. const splitDate = (dateStr) =>{
  32. const splitDate = dateStr.split(".")
  33. return {
  34. day: parseInt(splitDate[0], 10),
  35. month:parseInt(splitDate[1], 10),
  36. year: parseInt(splitDate[2], 10)
  37. }
  38. }
  39.  
  40. console.log(calculateEndDateOfContract("15.09.2000", "15.09.2018", 65));
  41. console.log(calculateEndDateOfContract("16.09.2000", "15.09.2018", 65));
  42. console.log(calculateEndDateOfContract("14.09.2000", "15.09.2018", 65));
Add Comment
Please, Sign In to add comment