8

I need to compare two different datetime strings (formed: YYYY-MM-DD'T'HH:mm).

Here are the datetime strings:

var a = ("2017-05-02T10:45");
var b = ("2017-05-02T12:15");

I've sliced the dates out of them so I only need the time (formed: HH:mm).

var now = a.slice(11, 16);
var then = b.slice(11, 16);

// now = 10:45
// then = 12:15

Is there any way I could get the difference between these two times?

Result should look like this:

1 hour 30 minutes

Also if the dates are different is there any easy solution to get the date difference too?

hindmost
  • 7,125
  • 3
  • 27
  • 39
IlariM
  • 376
  • 1
  • 3
  • 16

3 Answers3

6

Use javascript Date:

var a = ("2017-05-02T10:45");
var b = ("2017-05-02T12:15");
var milliseconds = ((new Date(a)) - (new Date(b)));

var minutes = milliseconds / (60000);
2oppin
  • 1,941
  • 20
  • 33
4

This should get you started:

d1 = new Date(Date.parse("2017-05-02T10:45"));
d2 = new Date(Date.parse("2017-05-02T12:15"));

var getDuration = function(d1, d2) {
    d3 = new Date(d2 - d1);
    d0 = new Date(0);

    return {
        getHours: function(){
            return d3.getHours() - d0.getHours();
        },
        getMinutes: function(){
            return d3.getMinutes() - d0.getMinutes();
        },
        getMilliseconds: function() {
            return d3.getMilliseconds() - d0.getMilliseconds();
        },
        toString: function(){
            return this.getHours() + ":" +
                   this.getMinutes() + ":" + 
                   this.getMilliseconds();
        },
    };
}

diff = getDuration(d1, d2);

console.log(diff.toString());

or use momentjs because, 1. it is well tested and bugs are tracked. 2. Coding from scratch is a fun learning experience but if you are in a corporate enviroment, coding from scratch will waste time (and thus money).

mika
  • 420
  • 3
  • 12
  • Thanks, this worked great. – IlariM May 02 '17 at 10:18
  • you're welcome. Coding from scratch is a fun learning experience but I really suggest you use [momentjs](http://momentjs.com/) – mika May 02 '17 at 10:24
  • `new Date(Date.parse("2017-05-02T10:45"))` is identical to `new Date("2017-05-02T10:45")`, just more to type. This also fails if the duration is greater than 24 hours. Far better to get the difference in milliseconds then manually convert to the required time format. – RobG May 03 '17 at 04:41
  • 1
    Downvoted because you suggest to use momentjs, but never give the slightest clue on why. Your code is magnitudes smaller in file size than even the minified version of that library (16.3k at the time of writing), so what could possibly justify using that over a simple script as the one you provided here, or the one in 2oppin's answer? – TheThirdMan Oct 15 '17 at 10:18
  • @TheThirdMan moment.js is used by thousands of people, it is well tested, and bugs are tracked. As I have commented earlier, Coding from scratch is a fun learning experience but if you are in a corporate enviroment, coding from scratch will waste time (and thus money). – mika Dec 20 '17 at 08:41
  • 1
    You're not entirely wrong, and if you were to add that to your answer, I'd undo my downvote. Nevertheless, approaching every problem in this fashion leads to bulky web pages, and giving first priority to getting results fast while leaving performance and user experience in the dust will get you a webpage that won't be worth what a customer pays for it, no matter how "cheap". For most libraries, you need to figure out how to implement them at first anyway (and also how to keep them and your code up to date), so especially for simple things like this, it might be better to just do it yourself. – TheThirdMan Dec 21 '17 at 00:51
1

i have a lib to make this simple:

wiki:https://github.com/jiangbai333/Common-tools/wiki/format code:https://github.com/jiangbai333/Common-tools/blob/dev/string/string.js

include string.js in your file, Then:

var temp = "short-stamp".format(+new Date("2017-05-02T12:15")) - "short-stamp".format(+new Date("2017-05-02T10:45"));

console.log(parseInt(temp / 3600), "hour", parseInt(temp % 3600 / 60), "minutes")