在momentjs中两次之间的小时差异(HH:MM:SS a)

时间:2021-07-17 21:33:15

I have two time without date

我有两次没有约会

var startTime="12:16:59 am";
var endTime="06:12:07 pm";

I want to show the total hours in between the above times by using moment.js.

我想用moment.js显示上述时间之间的总小时数。

If it's not possible in moment.js then please let me know using by javascript.

如果在moment.js中不可能,那么请通过javascript告诉我。

Inputs:

输入:

var startTime="01:30:00 am";
var endTime="2:45:07 pm";

Expected Output:

预期产出:

1 hour and 15 minutes

8 个解决方案

#1


22  

try below code

尝试以下代码

var startTime=moment("12:16:59 am", "HH:mm:ss a");
var endTime=moment("06:12:07 pm", "HH:mm:ss a");
var duration = moment.duration(endTime.diff(startTime));
var hours = parseInt(duration.asHours());
var minutes = parseInt(duration.asMinutes())%60;
alert (hours + ' hour and '+ minutes+' minutes.');

check fiddle here http://jsfiddle.net/nil4you/gs69Lv5x/

在这里检查小提琴http://jsfiddle.net/nil4you/gs69Lv5x/

#2


11  

Get Hours

I got the hours by using this code

我通过使用此代码获得了时间

endTime.diff(startTime, 'hours')

Get Minutes

i got the minutes by using this below code

我使用下面的代码得到了会议记录

var mins = moment.utc(moment(endTime, "HH:mm:ss").diff(moment(startTime, "HH:mm:ss"))).format("mm")

My Working code is

$scope.UpdateTimeSheet = function (rowEntity) {   


            if (rowEntity.StartTime.toString().length != 11) {
                rowEntity.StartTime = moment(rowEntity.StartTime).format("hh:mm:ss a");
            }
            if (rowEntity.EndTime.toString().length != 11) {
                rowEntity.EndTime = moment(rowEntity.EndTime).format("hh:mm:ss a");
            }

            var startTime = moment(rowEntity.StartTime, "hh:mm:ss a");
            var endTime = moment(rowEntity.EndTime, "hh:mm:ss a");

            var mins = moment.utc(moment(endTime, "HH:mm:ss").diff(moment(startTime, "HH:mm:ss"))).format("mm")

            rowEntity.TotalHours = endTime.diff(startTime, 'hours') + " Hrs and " + mins + " Mns";

}

#3


4  

var startTime = moment("12:16:59 am", 'hh:mm:ss a');
var endTime = moment("06:12:07 pm", 'hh:mm:ss a');

endTime.diff(startTime, 'hours');

#4


2  

var start = moment.duration("09:45", "HH:mm");
var end = moment.duration("10:30", "HH:mm");
var diff = end.subtract(start);
diff.hours(); // return hours
diff.minutes(); // return minutes

#5


1  

var startTime = moment("12:16:59 am", 'hh:mm:ss a');
var endTime = moment("06:12:07 pm", 'hh:mm:ss a');

var totalHours = (endTime.diff(startTime, 'hours'));
var totalMinutes = endTime.diff(startTime, 'minutes');
var clearMinutes = totalMinutes % 60;
console.log(totalHours + " hours and " + clearMinutes + " minutes");

#6


0  

For - "12:00:01" Format without am, pm format following os the code..

对于 - “12:00:01”格式没有am,pm格式跟随代码..

   var startTime = moment('12:00:01', 'hh:mm:ss a');
   var endTime = moment('13:00:10' , 'hh:mm:ss a');
   var totalHours = (endTime.diff(startTime, 'hours'));
   var totalMinutes = endTime.diff(startTime, 'minutes');
   var clearMinutes = totalMinutes % 60;
   alert(totalHours + " hours and " + clearMinutes + " minutes");

#7


-1  

var startTime = moment("12:16:59");
var endTime = moment("06:12:07");

var duration = moment.duration(endTime.diff(startTime));
var hours = duration.asHours();

#8


-2  

I know this is marked as already answered but you can literally just do...

我知道这标记为已经回答,但你可以真正做到......

moment.utc(moment.duration(moment(dateA).diff(moment(dateB))).asMilliseconds()).format('HH:mm:ss');

#1


22  

try below code

尝试以下代码

var startTime=moment("12:16:59 am", "HH:mm:ss a");
var endTime=moment("06:12:07 pm", "HH:mm:ss a");
var duration = moment.duration(endTime.diff(startTime));
var hours = parseInt(duration.asHours());
var minutes = parseInt(duration.asMinutes())%60;
alert (hours + ' hour and '+ minutes+' minutes.');

check fiddle here http://jsfiddle.net/nil4you/gs69Lv5x/

在这里检查小提琴http://jsfiddle.net/nil4you/gs69Lv5x/

#2


11  

Get Hours

I got the hours by using this code

我通过使用此代码获得了时间

endTime.diff(startTime, 'hours')

Get Minutes

i got the minutes by using this below code

我使用下面的代码得到了会议记录

var mins = moment.utc(moment(endTime, "HH:mm:ss").diff(moment(startTime, "HH:mm:ss"))).format("mm")

My Working code is

$scope.UpdateTimeSheet = function (rowEntity) {   


            if (rowEntity.StartTime.toString().length != 11) {
                rowEntity.StartTime = moment(rowEntity.StartTime).format("hh:mm:ss a");
            }
            if (rowEntity.EndTime.toString().length != 11) {
                rowEntity.EndTime = moment(rowEntity.EndTime).format("hh:mm:ss a");
            }

            var startTime = moment(rowEntity.StartTime, "hh:mm:ss a");
            var endTime = moment(rowEntity.EndTime, "hh:mm:ss a");

            var mins = moment.utc(moment(endTime, "HH:mm:ss").diff(moment(startTime, "HH:mm:ss"))).format("mm")

            rowEntity.TotalHours = endTime.diff(startTime, 'hours') + " Hrs and " + mins + " Mns";

}

#3


4  

var startTime = moment("12:16:59 am", 'hh:mm:ss a');
var endTime = moment("06:12:07 pm", 'hh:mm:ss a');

endTime.diff(startTime, 'hours');

#4


2  

var start = moment.duration("09:45", "HH:mm");
var end = moment.duration("10:30", "HH:mm");
var diff = end.subtract(start);
diff.hours(); // return hours
diff.minutes(); // return minutes

#5


1  

var startTime = moment("12:16:59 am", 'hh:mm:ss a');
var endTime = moment("06:12:07 pm", 'hh:mm:ss a');

var totalHours = (endTime.diff(startTime, 'hours'));
var totalMinutes = endTime.diff(startTime, 'minutes');
var clearMinutes = totalMinutes % 60;
console.log(totalHours + " hours and " + clearMinutes + " minutes");

#6


0  

For - "12:00:01" Format without am, pm format following os the code..

对于 - “12:00:01”格式没有am,pm格式跟随代码..

   var startTime = moment('12:00:01', 'hh:mm:ss a');
   var endTime = moment('13:00:10' , 'hh:mm:ss a');
   var totalHours = (endTime.diff(startTime, 'hours'));
   var totalMinutes = endTime.diff(startTime, 'minutes');
   var clearMinutes = totalMinutes % 60;
   alert(totalHours + " hours and " + clearMinutes + " minutes");

#7


-1  

var startTime = moment("12:16:59");
var endTime = moment("06:12:07");

var duration = moment.duration(endTime.diff(startTime));
var hours = duration.asHours();

#8


-2  

I know this is marked as already answered but you can literally just do...

我知道这标记为已经回答,但你可以真正做到......

moment.utc(moment.duration(moment(dateA).diff(moment(dateB))).asMilliseconds()).format('HH:mm:ss');