How do I get the difference between two Dates in JavaScript?

前端 未结 16 2554
北海茫月
北海茫月 2020-11-22 03:03

I\'m creating an application which lets you define events with a time frame. I want to automatically fill in the end date when the user selects or changes the start date.

16条回答
  •  萌比男神i
    2020-11-22 04:08

    Thanks @Vincent Robert, I ended up using your basic example, though it's actually newBegin + oldEnd - oldBegin. Here's the simplified end solution:

        // don't update end date if there's already an end date but not an old start date
        if (!oldEnd || oldBegin) {
            var selectedDateSpan = 1800000; // 30 minutes
            if (oldEnd) {
                selectedDateSpan = oldEnd - oldBegin;
            }
    
           newEnd = new Date(newBegin.getTime() + selectedDateSpan));
        }
    

提交回复
热议问题