Eric Mitjans
Eric Mitjans

Reputation: 2179

Issue when adding momentJS objects in AngularJS

I have an object that looks like this:

var carsDict = { 
391: [
    {
     arrival: "17.43.49",
     car: "391",
     date: "11/11/2014",
     duration: "00:00:06",
     time: "17.43.43"
    }
],

396: {
    0: {
     arrival: "17.20.48",
     car: "396",
     date: "11/11/2014",
     duration: "00:00:11",
     time: "17.20.37",
    },        
    1: {
     arrival: "17.21.27",
     car: "396",
     date: "11/11/2014",
     duration: "00:00:17",
     time: "17.21.10"
    }
}
};

The duration property is a MomentJS object constructed by the following line:

record.duration = moment.utc(moment(record.arrival,'HH:mm:ss')
                  .diff(moment(record.time,'HH:mm:ss'))).format("HH:mm:ss");

The next step is to add all duration objects from each array together. I get to do that by using the following piece of code:

for (record in carsDict) {     
  var totalduration = '';

  for (item in carsDict[record]) {
    // get totalduration figured out..
    totalduration += moment.utc(carsDict[record][item].duration, 'HH:mm:ss');
  }
    
  for (item in carsDict[record]) {
    // now add totalduration to each record        
    carsDict[record][item]['totalduration'] = totalduration; 
  }
};

Given the example above, the totalduration for the first array should be "00:00:06" and for the second "00:00:28" ("00:00:11" + "00:00:17"), but I'm getting other results, 1415750406000 for the first one, and 14157504110001415750417000 for the second one.

I assume the error is in this line, but I can't see exactly why is not adding the moment() objects correctly.

totalduration += moment.utc(carsDict[record][item].duration, 'HH:mm:ss');

Here's a JSFiddle.

Any idea about what am I doing wrong?

Upvotes: 0

Views: 184

Answers (1)

yerforkferchips
yerforkferchips

Reputation: 1985

Your problem is in fact not located in the line you think, but in these two lines:

var totalduration = '';
totalduration += moment.utc(carsDict[record][item].duration, 'HH:mm:ss');

You're starting with a string and adding a moment/date object to it. If you use + with a string and any other object, it will first convert that other object to a primitive, then a string, and then concatenate both:

"1" + 0 === "10"

So the result you're getting is caused by the moment object first being converted to a primitive - in this case a number (you can confirm this by calling valueOf on the moment object) - and then a string representation of this number.

What you need to do is calculate the total time difference in milliseconds, so start with a number:

var totalduration = 0;

and convert the moment object to a number before adding it:

totalduration += Number(moment.utc(carsDict[record][item].duration, 'HH:mm:ss'));

then parse this number (which is a Unix timestamp, or the total sum in milliseconds) using moment and format it when setting:

carsDict[record][item]['totalduration'] = moment.utc(totalduration).format('HH:mm:ss'); 

Upvotes: 1

Related Questions