0

I have two variables formated like this

2017-09-27 16:26:39

2017-09-28 06:30:00
Adaleni
  • 966
  • 7
  • 15

3 Answers3

1

You could use Date.parse to convert your variables to unix timestamps. Then you could compare the two timestamps like as follows.

var unixtimeOne = Date.parse("2017-09-27 16:26:39");
var unixtimeTwo = Date.parse("2017-09-28 06:30:00");

console.log(unixtimeOne);
console.log(unixtimeTwo);
  
// You could compare the timestamps as follows
console.log(unixtimeOne < unixtimeTwo)
Paul Fitzgerald
  • 11,770
  • 4
  • 42
  • 54
0

    var date1 = new Date('2017 09-27 16:26:39');
    var date2 = new Date('2017-09-28 06:30:00');

    var msecs1 = date1.getTime(); // in milliseconds
    var msecs2 = date2.getTime(); // in milliseconds

    console.log(msecs1 - msecs2); // -50601000
Deano
  • 11,582
  • 18
  • 69
  • 119
Artem Arkhipov
  • 7,025
  • 5
  • 30
  • 52
0

If you are willing to use moment.js, it gives you great comparison methods such as 'isSame', 'isAfter' and 'isBefore':

var x = moment('2017-09-27 16:26:39');
var y = moment('2017-09-28 06:30:00');

x.isSame(y) //false
x.isAfter(y) //false
x.isBefore(y) //true

To find the time difference between the two datetimes:

y.diff(x, 'hours') //14
pro
  • 792
  • 7
  • 30