showing results for - "javascript prootmoion"
Davide
30 May 2018
1 const hoursToDay = (hours) => Math.abs(Math.floor(hours / 24))
2
3
4const findHour = (pickUpDatetime, dropOffDatetime) =>
5	Math.abs(new Date(pickUpDatetime).getTime() - new Date(dropOffDatetime).getTime()) / 36e5
6
7
8const findOverPromotionDay = (
9	reservationStart,
10	reservationEnd,
11	pickupDateTime,
12	dropoffDateTime
13) => {
14	const dateNow = new Date('2021/06/21')
15
16	const reservationPromotion = hoursToDay(findHour(reservationStart, reservationEnd))
17	const reservationPromotionLive = hoursToDay(findHour(reservationStart, dateNow))
18
19	const reservation = hoursToDay(findHour(pickupDateTime, dropoffDateTime))
20	const reservationLive = hoursToDay(findHour(pickupDateTime, dateNow))
21
22	const subtractPromotion = reservationPromotion - reservationPromotionLive
23	const subtractReservation = reservation - reservationLive - subtractPromotion
24
25	if (subtractPromotion >= subtractReservation) return true
26	else return false
27}
28
29const promotions = [
30  {
31    reservationStart: '2021/06/1',
32    reservationEnd: '2021/06/15',
33    minimumDays: 4
34  },
35  {
36    reservationStart: '2021/06/1',
37    reservationEnd: '2021/06/16',
38    minimumDays: 5
39  },
40  {
41    reservationStart: '2021/06/1',
42    reservationEnd: '2021/06/22',
43    minimumDays: 8
44  }
45]
46
47const reservationPromotionStart = new Date('2021/06/1')
48const reservationPromotionEnd = new Date('2021/06/22')
49
50const reservationStart = new Date('2021/06/1')
51const reservationEnd = new Date('2021/06/23')
52
53console.log(findOverPromotionDay(reservationPromotionStart, 
54                                 reservationPromotionEnd,
55                                 reservationStart, reservationEnd))