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
2 Answers
Reset to default 6This 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
版权声明:本文标题:javascript - How to get total hours HH:MM format using Moment.js? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741678912a2392048.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论