0

Suppose users trip start from 07/06/2019 and its end on 14/07/2019.

so on 07/06/2019 day will be = 1 (today is 1 day of your trip) on 08/06/2019 day will be =2 (today is second of your trip) . so on

I try to do somthing like this but its not working.

 let startDay =moment('9.6.2019', 'DD.MM.YYYY')
        let today =moment()
        let endDay = moment('10.6.2019', 'DD.MM.YYYY')
        let start_to_today_days = today.diff(startDay, 'days')
        let start_to_end_days = endDay.diff(startDay, 'days')
        let actDay =  parseInt(start_to_end_days)- 
        parseInt(start_to_today_days)
        let expendday = parseInt(start_to_today_days) - parseInt(actDay)
        console.log(Math.abs(expendday))

user booked trip today for 7/06/2019 to 14/07/2109. day counter will start from tomorrow and count elapsed days until end date reached.

abhay
  • 642
  • 5
  • 13
  • "It's not working." If it's not working, then you should fix it. – Michael Lorton Jun 06 '19 at 07:16
  • Possible duplicate of [How do I get the number of days between two dates in JavaScript?](https://stackoverflow.com/questions/542938/how-do-i-get-the-number-of-days-between-two-dates-in-javascript) – Dzhuneyt Jun 06 '19 at 07:17

5 Answers5

1

You could create a function using moment.js to calculate elapsed times.

// Day of trip is the trip duration + 1..
function getDayOfTrip(tripStartString, currentDateString) {
    let startDate = moment(tripStartString, 'DD.MM.YYYY');
    let currentDate = moment(currentDateString, 'DD.MM.YYYY');
    return currentDate.diff(startDate, 'days') + 1;
}

let tripStartTime = '7.6.2019';
let tripDates = ['7.6.2019', '8.6.2019', '9.6.2019', '10.6.2019', '11.6.2019', '12.6.2019'];

tripDates.forEach(tripDate => console.log(`Trip date: ${tripDate}, day of trip: ${getDayOfTrip(tripStartTime, tripDate)}`));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.25/moment-timezone-with-data-10-year-range.js"></script>
Terry Lennox
  • 29,471
  • 5
  • 28
  • 40
0

The diff between 2 days will give you the number of milliseconds and with the bellow calculation you can get the number of days:

let startDay =moment('9.6.2019', 'DD.MM.YYYY')
let today =moment()
let endDay = moment('10.6.2019', 'DD.MM.YYYY')
let days = Math.floor(parseInt(endDay - startDay)/(24*60*60*1000));
console.log(Math.abs(days))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.js"></script>
Alon Yampolski
  • 851
  • 7
  • 15
0

Use below function code to find out no of days elapsed between two dates

function dayElapsed(firstDate , secondDate) {
    let day , month , year;         

    const firstArr = firstDate.split('.');
    const secondArr = secondDate.split('.');

    day = Math.abs(parseInt(firstArr[0]) - parseInt(secondArr[0]));    

    month = Math.abs(parseInt(firstArr[1]) - parseInt(secondArr[1]));    

    year = Math.abs(parseInt(firstArr[2]) - parseInt(secondArr[2]));    

   return (day + month*30 + year*365); 
 }

// call to function
dayElapsed('06.06.2019' , '08.06.2019')
Sunny Goel
  • 1,982
  • 2
  • 15
  • 21
0

I believe that Javascript has a built in Date.

let start = new Date(2019, 5, 7); // (year, month - 1, day)
let end = new Date(2019, 6, 14);

let diff = end - start; // diff is milliseconds
let day = diff / 86400000; // 86400000: milliseconds per day
Bi Ao
  • 704
  • 5
  • 11
0

fixed

     tripElapsedDaysOutLayCounter(s, e) {
    const COMING_SOON = "COMING SOON",
        TRIP_COMPLETED = "TRIP COMPLETED",
        INVALID_DATES = "INVALID DATES",
        DAY_ONE = 1

    let today = moment(new Date().toISOString().slice(0, 10).replace(/-/g, "."), 'YYYY.MM.DD')
    let startDay = moment(s, 'YYYY.MM.DD')
    let endDay = moment(e, 'YYYY.MM.DD')
    let end_start_day = endDay.diff(startDay, 'days')
    let today_to_end_day = today.diff(endDay, 'days')
    if (startDay.isValid() && endDay.isValid()) {
        if (startDay.isAfter(today) && endDay.isAfter(today)) {
            return COMING_SOON
        } else if (startDay.isBefore(today) && endDay.isBefore(today)) {
            return TRIP_COMPLETED
        } else if (startDay.isSame(today) && endDay.isAfter(today)) {
            return DAY_ONE
        } else if (startDay.isSame(endDay)) {
            return DAY_ONE
        } else {
            return Math.abs(Math.abs(end_start_day) - 
    Math.abs(today_to_end_day))+1
        }
    } else {
        return INVALID_DATES
    }

}

Thank you everyone for your precious answers

abhay
  • 642
  • 5
  • 13