我有两次没有约会
var startTime="12:16:59 am";
var endTime="06:12:07 pm";
我想使用moment.js.
如果moment.js
无效,请通过javascript
告诉我们。
输入
var startTime="01:30:00 am";
var endTime="2:45:07 pm";
预期输出:
1 hour and 15 minutes
答案 0 :(得分:28)
尝试以下代码
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.');
答案 1 :(得分:16)
我通过使用此代码获得了时间
endTime.diff(startTime, 'hours')
我使用下面的代码
获得了会议记录var mins = moment.utc(moment(endTime, "HH:mm:ss").diff(moment(startTime, "HH:mm:ss"))).format("mm")
$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";
}
答案 2 :(得分:7)
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');
答案 3 :(得分:3)
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
答案 4 :(得分:2)
这对我有用;迄今为止最好的解决方案
>>> sup_a = super(A, b)
>>> sup_a
<super: <class 'A'>, <B object>>
>>> sup_a.__thisclass__, sup_a.__self_class__
(<class '__main__.A'>, <class '__main__.B'>)
答案 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 :(得分:1)
var startTime = moment("12:16:59 am", "HH:mm:ss a"),
endTime = moment("06:12:07 pm", "HH:mm:ss a");
//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());
//method 2
console.log('hrs: ', moment(endTime).diff(startTime, 'hours'));
console.log('dif in Mins: ', moment(endTime).diff(moment(startTime), 'minutes'));
//method 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 formated output
console.log(dif.humanize());
答案 7 :(得分:0)
For - “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");
答案 8 :(得分:0)
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>
答案 9 :(得分:-1)
var fromtime = moment($('#fromtime').val(), "HH:mm");
var totime = moment($('#totime').val(), "HH:mm");
var dif = moment.duration(totime.diff(fromtime));
var diff = [dif.hours(), dif.minutes()].join(':');
if(diff != "NaN:NaN" && dif.hours()>=0 && dif.minutes()>=0){
$('#duration').text([dif.hours(), dif.minutes()].join(':'));
}else{
$('#duration').text('');
}
答案 10 :(得分:-2)
var startTime = moment("12:16:59");
var endTime = moment("06:12:07");
var duration = moment.duration(endTime.diff(startTime));
var hours = duration.asHours();
答案 11 :(得分:-2)
我知道这已被标记为已经回答,但你可以真正做到......
moment.utc(moment.duration(moment(dateA).diff(moment(dateB))).asMilliseconds()).format('HH:mm:ss');