更新时间:2023-01-31 14:23:35
您可以使用 moment.js 创建一个函数来计算经过的时间.
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>