admin管理员组

文章数量:1301544

I want to calculate the week total time using Moment.js. Please advise. This is what I tried so far:

total = "00:00";
for (var i in json_obj) {

    var timeEnd = json_obj[i].END;
    var timeStart = json_obj[i].START;

    var timeDiff = moment.utc(moment(timeEnd, "HH:mm:ss").diff(moment(timeStart, "HH:mm:ss"))).format("HH:mm");
    var res = timeDiff;
    total = moment().add(res);

I want to calculate the week total time using Moment.js. Please advise. This is what I tried so far:

total = "00:00";
for (var i in json_obj) {

    var timeEnd = json_obj[i].END;
    var timeStart = json_obj[i].START;

    var timeDiff = moment.utc(moment(timeEnd, "HH:mm:ss").diff(moment(timeStart, "HH:mm:ss"))).format("HH:mm");
    var res = timeDiff;
    total = moment().add(res);
Share edited Jan 19, 2016 at 6:24 asked Jan 19, 2016 at 4:25 user5534204user5534204 8
  • What are the values of json_obj[i].END; and json_obj[i].START ? Are they seconds? – drj Commented Jan 19, 2016 at 4:30
  • Need to provide sample input data for anyone to be able to help. If all you have is "HH:mm:ss" there is no way to know what day those occurred on. Need full dateTime to do that – charlietfl Commented Jan 19, 2016 at 4:32
  • @drj : 06:00:00 its hh:mm:ss format. i have update the question with screenshot – user5534204 Commented Jan 19, 2016 at 4:37
  • @charlietfl : pls check updated question with screenshot – user5534204 Commented Jan 19, 2016 at 4:38
  • screenshot doesn't do us any good... can't extract data from image. Best thing to do is create a demo with enough data and script dependencies to run your code ... jsfiddle , plnkr.co, codepen.io etc can be used – charlietfl Commented Jan 19, 2016 at 4:39
 |  Show 3 more ments

2 Answers 2

Reset to default 6

This is what i have achieved with the help of SO and moment.js documentation.

var startTime = moment("06:10:00", 'hh:mm');
var endTime = moment("08:00:00", 'hh:mm');

var totalSec = endTime.diff(startTime, 'seconds');

var hours = parseInt(totalSec / 3600) % 24;
var minutes = parseInt(totalSec / 60) % 60;
var seconds = totalSec % 60;

var result = (hours < 10 ? "0" + hours : hours) + ":" + (minutes < 10 ? "0" + minutes : minutes) + ":" + (seconds < 10 ? "0" + seconds : seconds);

The solution is not wholly implemented with moment but bining old school logic with momentjs.

But this will fulfill your requirement.

OR

To remove old school logic and truly go with moment use below code.

var result = moment().startOf('day')
        .seconds(totalSec)
        .format('H:mm')

JSFiddle with both version.

The best solution which will give the exact result. Even if your hours will be in 3 digits then it will give the proper result.

const allhours= [
 '5677:40:20',
 '888:30:50'
]
const totalhours = allhours.slice(1)
.reduce((prev, cur) => moment.duration(cur).add(prev),
moment.duration(allhours[0]));
var ms = totalhours ._milliseconds;
var ticks = ms / 1000;
var hh = Math.floor(ticks / 3600);
var mm = Math.floor((ticks % 3600) / 60);
var ss = ticks % 60;


console.log(hh+""+mm+""+ss);

本文标签: javascriptHow to get total hours HHMM format using MomentjsStack Overflow