Jeśli chcesz mieć całkowitą liczbę minut między dwiema datami w ciągu dnia, niż może poniżej, kod pomoże Ci w pełni Data rozpoczęcia: 2018-05-04 02:08:05 , Data zakończenia: 2018-05-14 09:04:07 ...
function countDaysAndTimes(startDate,endDate){
return new Promise(function (resolve, reject) {
var dayObj = new Object;
var finalArray = new Array;
var datetime1 = moment(startDate);
var datetime2 = moment(endDate);
if(datetime1.format('D') != datetime2.format('D') || datetime1.format('M') != datetime2.format('M') || datetime1.format('YYYY') != datetime2.format('YYYY')){
var onlyDate1 = startDate.split(" ");
var onlyDate2 = endDate.split(" ");
var totalDays = moment(onlyDate2[0]).diff(moment(onlyDate1[0]), 'days')
// First Day Entry
dayObj.startDate = startDate;
dayObj.endDate = moment(onlyDate1[0]).add(1, 'day').format('YYYY-MM-DD')+" 00:00:00";
dayObj.minutes = moment(dayObj.endDate).diff(moment(dayObj.startDate), 'minutes');
finalArray.push(dayObj);
// Between Days Entry
var i = 1;
if(totalDays > 1){
for(i=1; i<totalDays; i++){
var dayObj1 = new Object;
dayObj1.startDate = moment(onlyDate1[0]).add(i, 'day').format('YYYY-MM-DD')+" 00:00:00";
dayObj1.endDate = moment(onlyDate1[0]).add(i+1, 'day').format('YYYY-MM-DD')+" 00:00:00";
dayObj1.minutes = moment(dayObj1.endDate).diff(moment(dayObj1.startDate), 'minutes');
finalArray.push(dayObj1);
}
}
// Last Day Entry
var dayObj2 = new Object;
dayObj2.startDate = moment(onlyDate1[0]).add(i, 'day').format('YYYY-MM-DD')+" 00:00:00";
dayObj2.endDate = endDate ;
dayObj2.minutes = moment(dayObj2.endDate).diff(moment(dayObj2.startDate), 'minutes');
finalArray.push(dayObj2);
}
else{
dayObj.startDate = startDate;
dayObj.endDate = endDate;
dayObj.minutes = datetime2.diff(datetime1, 'minutes');
finalArray.push(dayObj);
}
console.log(JSON.stringify(finalArray));
// console.table(finalArray);
resolve(finalArray);
});
}
Wynik
[
{
"startDate":"2018-05-04 02:08:05",
"endDate":"2018-05-05 00:00:00",
"minutes":1311
},
{
"startDate":"2018-05-05 00:00:00",
"endDate":"2018-05-06 00:00:00",
"minutes":1440
},
{
"startDate":"2018-05-06 00:00:00",
"endDate":"2018-05-07 00:00:00",
"minutes":1440
},
{
"startDate":"2018-05-07 00:00:00",
"endDate":"2018-05-08 00:00:00",
"minutes":1440
},
{
"startDate":"2018-05-08 00:00:00",
"endDate":"2018-05-09 00:00:00",
"minutes":1440
},
{
"startDate":"2018-05-09 00:00:00",
"endDate":"2018-05-10 00:00:00",
"minutes":1440
},
{
"startDate":"2018-05-10 00:00:00",
"endDate":"2018-05-11 00:00:00",
"minutes":1440
},
{
"startDate":"2018-05-11 00:00:00",
"endDate":"2018-05-12 00:00:00",
"minutes":1440
},
{
"startDate":"2018-05-12 00:00:00",
"endDate":"2018-05-13 00:00:00",
"minutes":1440
},
{
"startDate":"2018-05-13 00:00:00",
"endDate":"2018-05-14 00:00:00",
"minutes":1440
},
{
"startDate":"2018-05-14 00:00:00",
"endDate":"2018-05-14 09:04:07",
"minutes":544
}
]
startTime.diff(end, 'hours', true);
? The duration.asHours (); zwrócił 1, gdyby to było 25 godzin temu.