JVM
JVM

Reputation: 59

Error calculating time with javascript and moment js

I have a problem calculating the time with moment. js. The problem is that when I add or subtract time in "hh: mm: ss" format and it is more than 1 hour it does well, but when I add or subtract time for example 00:33:33 or less than one hour makes the calculations wrong. EXAMPLE THAT MAKES THE CALCULATIONS WRONG

var nDemo = '01:00:00'; 

var nCalculo = '00:13:00'; 
var timess= nDemo.split(":");

            var hh = timess= [0];
            var min = timess= [1];
            var ss = timess= [2];


            finalr = moment.utc(nCalculo, 'hh:mm:ss').subtract(hhr, 'seconds').format('hh:mm:ss');
            finalr1 = moment.utc(finalr, 'hh:mm:ss').subtract(minr, 'minutes').format('hh:mm:ss');
            finalr2 = moment.utc(finalr1, 'hh:mm:ss').subtract(ssr, 'hour').format('hh:mm:ss');
            alert (finalr2);

The result I get is: 12:47:00 when it should be 00:47:00. What am I missing?

If I make the same calculation in sum with amounts less than an hour it also makes a mistake.

var nDemo = '00:30:00'; 

var nCalculo = '00:13:00'; 
var timess= nDemo.split(":");

            var hh = timess= [0];
            var min = timess= [1];
            var ss = timess= [2];


            finalr = moment.utc(nCalculo, 'hh:mm:ss').add(hhr, 'hour').format('hh:mm:ss');
            finalr1 = moment.utc(finalr, 'hh:mm:ss').add(minr, 'minutes').format('hh:mm:ss');
            finalr2 = moment.utc(finalr1, 'hh:mm:ss').add(ssr, 'hour').format('hh:mm:ss');
            alert (finalr2);

The result I get is: 12:43:00 when it should be 00:43:00. What am I missing?

Any other type of calculation that you make above one hour is correct, the error comes up when you make calculations with less than one hour. Any ideas? Thank you

Upvotes: 2

Views: 159

Answers (2)

IsThisJavascript
IsThisJavascript

Reputation: 1716

The issue is because hh is a 12 hour clock from 01-12 as defined from here

You should instead opt for using H or HH instead which is 0-23 hour;

finalr = moment.utc(nCalculo, 'H:mm:ss').add(hhr, 'hour').format('H:mm:ss');
finalr1 = moment.utc(finalr, 'H:mm:ss').add(minr, 'minutes').format('H:mm:ss');
finalr2 = moment.utc(finalr1, 'H:mm:ss').add(ssr, 'hour').format('H:mm:ss');

Upvotes: 1

Rafael Paulino
Rafael Paulino

Reputation: 581

I agree with IsThisJavascript:

var m1 = moment.utc('01:00:00', 'HH:mm:ss')
var m2 = moment.utc('00:13:00', 'HH:mm:ss')
m1.subtract(m2.get('hour'), 'hour').subtract(m2.get('minute'), 'minute').subtract(m2.get('second'), 'second').format('HH:mm:ss')

But, pay attention, moment work with date and time. What do you want when you add for example: 23:59:59 with 1 hour? should you get 24:59:59? Or 00:59:59?

This way you may get 00:59:59 because it would be another day.

If you want the hours to be "continuous", then I would suggest you calculate manually (convert all to seconds, sum or subtract, them convert back to hour:min:sec format).

Upvotes: 0

Related Questions