Advertisement
aldikhan13

DYNAMIC DISCOUNT PRODUCT - UTILS

Nov 17th, 2020 (edited)
350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // kalau tanggalnya itu cocok berarti discount berakhir
  2. // kalau tanggalnya tidak cocok discount belum berakhir
  3.  
  4. function discountProduct(date, discountPrice, salePrice) {
  5.  
  6. let intMonth = 0
  7. const d= new Date()
  8. const month = ['January','February','March','April','May','June','July','August','September','October', 'November','December'];
  9. const dateMonth = [31,29,31,30,31,30,31,31,30,31,30,31]
  10. const dateNow = d.getDate()
  11. const monthIndex = month.indexOf(month[date.getMonth()])
  12. const dateMonthIndex = dateMonth[monthIndex]
  13. const discountDate = new Date(date)
  14.  
  15. for(let i = 0; i < dateMonthIndex; i++) {
  16.       if(dateNow === i) {
  17.         intMonth += i
  18.       }  
  19. }
  20.  
  21. const discountDateOver = discountDate.getDate() !== intMonth  ? discountPrice : salePrice
  22. return discountDateOver
  23. }
  24.  
  25.  
  26. const resultDiscountPrice = discountProduct('2020-11-17', true, false)
  27. console.log(resultDiscount);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement