私は日付なしで2つの時間を持っています
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
このコードを使用して時間を取得しました
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";
}
以下のコードを試してください
// 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.');
ここでフィドルを確認してください http://jsfiddle.net/nil4you/gs69Lv5x/
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');
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
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");
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());
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>
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");