Javascript 在 momentjs 中两次之间的小时差 (HH:MM:SS a)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29745873/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hour difference between two times(HH:MM:SS a)in momentjs
提问by Ramesh Rajendran
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.jsthen please let me know using by javascript.
如果它不可能, moment.js那么请让我知道使用 by javascript。
Inputs:
输入:
var startTime="01:30:00 am";
var endTime="2:45:07 pm";
Expected Output:
预期输出:
1 hour and 15 minutes
采纳答案by Ramesh Rajendran
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";
}
回答by nirmal
try below code
试试下面的代码
// start time and end time
var startTime = moment("12:16:59 am", "HH:mm:ss a");
var endTime = moment("06:12:07 pm", "HH:mm:ss a");
// calculate total duration
var duration = moment.duration(endTime.diff(startTime));
// duration in hours
var hours = parseInt(duration.asHours());
// duration in minutes
var minutes = parseInt(duration.asMinutes())%60;
alert (hours + ' hour and '+ minutes+' minutes.');
check fiddle here http://jsfiddle.net/nil4you/gs69Lv5x/
回答by Vigneswaran Marimuthu
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');
回答by Manish Thomas
//Get start time and end time
var startTime = moment("12:16:59 am", "HH:mm:ss a"),
endTime = moment("06:12:07 pm", "HH:mm:ss a");
1. Method 1
一、方法一
var dif = moment.duration(endTime.diff(startTime));
console.log([dif.hours(), dif.minutes(), dif.seconds()].join(':'));
console.log('dif in Mins: ', (dif.hours() * 60) + dif.minutes());
2. Method 2
2. 方法二
console.log('hrs: ', moment(endTime).diff(startTime, 'hours'));
console.log('dif in Mins: ', moment(endTime).diff(moment(startTime), 'minutes'));
3. Method 3
3.方法三
var hrs = moment.utc(endTime.diff(startTime)).format("HH");
var min = moment.utc(endTime.diff(startTime)).format("mm");
var sec = moment.utc(endTime.diff(startTime)).format("ss");
console.log([hrs, min, sec].join(':'));
4. Formatted output
4. 格式化输出
console.log(dif.humanize());
回答by Irfan Ashraf
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
回答by Tarik Fojnica
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");
回答by Mohideen bin Mohammed
myStart = "01:30:00 am";
myEnd = "2:45:07 am";
function getTimeDiff(start, end) {
return moment.duration(moment(end, "HH:mm:ss a").diff(moment(start, "HH:mm:ss a")));
}
diff = getTimeDiff(myStart, myEnd)
console.log(`${diff.hours()} Hour ${diff.minutes()} minutes`);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
回答by Sam
This worked for me; the best solution so far
这对我有用;迄今为止最好的解决方案
function calculateTimeDifference() {
var date1 = new Date();
var date2 = new Date();
var diff = date2.getTime() - date1.getTime();
var msec = diff;
var hh = `0${Math.floor(msec / 1000 / 60 / 60)}`;
msec -= hh * 1000 * 60 * 60;
var mm = `0${Math.floor(msec / 1000 / 60)}`;
msec -= mm * 1000 * 60;
var ss = `0${Math.floor(msec / 1000)}`;
msec -= ss * 1000;
return hh.slice(-2) + ":" + mm.slice(-2) + ":" + ss.slice(-2);
}
回答by Sibu Stephen
For - "12:00:01" Format without am, pm format following os the code..
对于 - "12:00:01" 格式不带 am, pm 格式跟随 os 代码..
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");
回答by Tyler V
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');

