如何计算两个日期之间的经过天数

时间:2019-06-06 07:15:00

标签: javascript vuejs2

假设用户的旅行始于2019年6月6日,结束于2019年7月14日。

所以在07/06/2019一天将是= 1(今天是您旅行的1天) 在08/06/2019一天将是= 2(今天是您旅行的第二天) 。如此

我尝试做这样的事情,但是它不起作用。

 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))

用户今天预订了7/07/2019至14/07/2109旅行。天计数器将从明天开始,并计算到结束日期为止经过的天数。

5 个答案:

答案 0 :(得分:1)

您可以使用moment.js创建一个函数来计算经过的时间。

// 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>

答案 1 :(得分:0)

2天之间的差异将为您提供毫秒数,并且通过下面的计算可以得到天数:

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>

答案 2 :(得分:0)

使用下面的功能代码找出两个日期之间没有经过的天数

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')

答案 3 :(得分:0)

我相信Javascript具有内置的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

答案 4 :(得分:0)

固定

     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
    }

}

谢谢大家的宝贵回答