Compare two dates Google apps script

Jacobvdb picture Jacobvdb · Jun 24, 2012 · Viewed 71.6k times · Source

What would be the best way to compare two dates?

var int = e.parameter.intlistbox;
var startDate = rSheet.getRange(parseInt(int) + 1 ,1).getValues();
// returns Sat Jun 30 2012 00:00:00 GMT-0300 (BRT) 
var toDay = new Date();
// Sat Jun 23 2012 22:24:56 GMT-0300 (BRT)

if (startDate > toDay){ ....

I saw the .toString() option but that seems to work only for == or === operator.

Anything clear about this matter?

Answer

megabyte1024 picture megabyte1024 · Jun 24, 2012

The Date object has the valueOf method which returns the number of milliseconds since midnight 1970-01-01. You can use it to compare dates. Something like

var date01 = new Date();
var date02 = new Date(2012, 5, 24);
if (date01.valueOf() > date02.valueOf()) {
   ....
}