How To Calculate The Elapsed Days Between Two Dates
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 o
Solution 1:
You could create a function using moment.js to calculate elapsed times.
// Day of trip is the trip duration + 1..functiongetDayOfTrip(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)}`));
<scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.25/moment-timezone-with-data-10-year-range.js"></script>
Solution 2:
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))
<scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.js"></script>
Solution 3:
Use below function code to find out no of days elapsed between two dates
functiondayElapsed(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 functiondayElapsed('06.06.2019' , '08.06.2019')
Solution 4:
I believe that Javascript has a built in Date.
let start=newDate(2019, 5, 7); // (year, month-1, day)
let end=newDate(2019, 6, 14);
let diff =end-start; // diff is milliseconds
let day= diff /86400000; //86400000: milliseconds perday
Solution 5:
fixed
tripElapsedDaysOutLayCounter(s, e) {
constCOMING_SOON = "COMING SOON",
TRIP_COMPLETED = "TRIP COMPLETED",
INVALID_DATES = "INVALID DATES",
DAY_ONE = 1let today = moment(newDate().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)) {
returnCOMING_SOON
} elseif (startDay.isBefore(today) && endDay.isBefore(today)) {
returnTRIP_COMPLETED
} elseif (startDay.isSame(today) && endDay.isAfter(today)) {
returnDAY_ONE
} elseif (startDay.isSame(endDay)) {
returnDAY_ONE
} else {
returnMath.abs(Math.abs(end_start_day) -
Math.abs(today_to_end_day))+1
}
} else {
returnINVALID_DATES
}
}
Thank you everyone for your precious answers
Post a Comment for "How To Calculate The Elapsed Days Between Two Dates"