const hoursToDay = (hours) => Math.abs(Math.floor(hours / 24))
const findHour = (pickUpDatetime, dropOffDatetime) =>
Math.abs(new Date(pickUpDatetime).getTime() - new Date(dropOffDatetime).getTime()) / 36e5
const findOverPromotionDay = (
reservationStart,
reservationEnd,
pickupDateTime,
dropoffDateTime
) => {
const dateNow = new Date('2021/06/21')
const reservationPromotion = hoursToDay(findHour(reservationStart, reservationEnd))
const reservationPromotionLive = hoursToDay(findHour(reservationStart, dateNow))
const reservation = hoursToDay(findHour(pickupDateTime, dropoffDateTime))
const reservationLive = hoursToDay(findHour(pickupDateTime, dateNow))
const subtractPromotion = reservationPromotion - reservationPromotionLive
const subtractReservation = reservation - reservationLive - subtractPromotion
if (subtractPromotion >= subtractReservation) return true
else return false
}
const promotions = [
{
reservationStart: '2021/06/1',
reservationEnd: '2021/06/15',
minimumDays: 4
},
{
reservationStart: '2021/06/1',
reservationEnd: '2021/06/16',
minimumDays: 5
},
{
reservationStart: '2021/06/1',
reservationEnd: '2021/06/22',
minimumDays: 8
}
]
const reservationPromotionStart = new Date('2021/06/1')
const reservationPromotionEnd = new Date('2021/06/22')
const reservationStart = new Date('2021/06/1')
const reservationEnd = new Date('2021/06/23')
console.log(findOverPromotionDay(reservationPromotionStart,
reservationPromotionEnd,
reservationStart, reservationEnd))