wake-up-neo.com

Stundendifferenz zwischen zwei Zeitpunkten (HH: MM: SS a) in momentjs

Ich habe zwei mal ohne Datum

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

Ich möchte die Gesamtstunden zwischen den obigen Zeiten anzeigen, indem Sie moment.js. verwenden.

Falls dies in moment.js nicht möglich ist, teilen Sie mir dies bitte mit javascript mit.

Eingänge:

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

Erwartete Ausgabe:  

1 hour and 15 minutes
16

Stunden bekommen

Ich habe die Stunden mit diesem Code bekommen

endTime.diff(startTime, 'hours')

Holen Sie sich Minuten

ich habe die Minuten mit diesem Code erhalten

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

Mein Arbeitscode ist

$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";

}
14

versuche den Code unten

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.');

gehe hier nach http://jsfiddle.net/nil4you/gs69Lv5x/

27
nirmal
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
3
Irfan Ashraf
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");
1
Tarik Fojnica
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());
1
Manish Thomas
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>

Für - "12:00:01" Format ohne am, pm Format nach dem Code ..

   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");
0
Sibu Stephen