0

I need to calculate the exact time diffrence using moment JS .

My JS code is :

var a = moment(from_url);
                a.format('DD/MM/YYYY hh:mm:ss');
                var b = moment(to_url);
                b.format('DD/MM/YYYY hh:mm:ss');

                console.log("from URL")
                var from_hours = from_url()
                console.log(b.diff(a, 'minutes')) // 
                console.log(b.diff(a, 'hours')) // 
                console.log(b.diff(a, 'days')) // 
                console.log(b.diff(a, 'weeks')) // 

                console.log("Time interval: "+b.diff(a, 'days')+ " days "+ b.diff(a, 'hours') +" hours " +b.diff(a, 'minutes')+" mintes");

Now,

from_url = '2016-05-03T08:00:00';
to_url = '2016-05-04T09:00:00';

Now, for these timings, I am getting output as : Time interval: 1 days 25 hours 1500 minutes It is converting everything to the days (1 day ~25 hours).

However, the output that I want is : 1 day 1 hour 0 minutes.

Can anyone please help me in this ? I am newbie to JS and unable to figure this out. Thanks.

learntogrow-growtolearn
  • 1,190
  • 5
  • 13
  • 37
  • 1
    There are many questions on this already, [*pick a duplicate*](http://stackoverflow.com/search?q=%5Bjavascript%5D+date+difference+in+days). The moment.js [*diff*](http://momentjs.com/docs/#/displaying/difference/) method only resolves the difference in one unit, not multiple. – RobG May 10 '16 at 00:22

1 Answers1

1

To parse strings, you should always tell the parser the format you are providing. Moment.js can help with that, but you can also use your own small functions.

For getting the difference in days, hours, etc. you can simply subtract one date from another to get milliseconds, then convert that to the format you want.

Note that date arithmetic is not simple, there are many rules that change depending on custom or administrative rules. Also, when going over daylight saving boundaries, some days are 23 hours long and some 25.

The following is a simple approach that does not take account of daylight saving. Hopefully the comments are sufficient, play with the output format to get whatever you need.

// Parse ISO format string as local, ignore timezone
// E.g. 2016-05-29T23:32:15
function parseISOLocal(s) {
  // Split string into its parts
  var b = s.split(/\D/);
  // Create and return a date object
  return new Date(b[0], b[1]-1, b[2], b[3], b[4], b[5]);
}

// Convert a millisecond value to days, hours, minutes and seconds
function formatDHMS(ms) {
  // Helper to add 's' to a number if other than 1
  function addS(n){return n == 1? '' : 's';}
   
  // Get the whole values of each unit, rounded down ( |0 truncates)
  var d =  ms/8.64e7 | 0;          // days
  var h = (ms%8.64e7) / 3.6e6 | 0; // hours
  var m = (ms%3.6e6)  / 6e4 | 0;   // minutes
  var s = (ms%6e4)    / 1e3 | 0;   // seconds

  // Return a formatted string
  return d + ' day' + addS(d) + ', ' + 
         h + ' hour' + addS(h) + ', ' + 
         m + ' minute' + addS(m) + ' and ' +
         s + ' second' + addS(s);
}

document.write(formatDHMS(parseISOLocal('2016-05-04T09:00:00') - parseISOLocal('2016-05-03T08:00:00')))
RobG
  • 142,382
  • 31
  • 172
  • 209