1

I've got these dates

var d1 = new Date(2019, 9, 6);
var d2 = new Date(2019, 9, 7);

How do I compare them to see which is later and which is earlier?

Username
  • 3,463
  • 11
  • 68
  • 111
  • 1
    With either [getTime()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTime) or [valueOf()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/valueOf). They both return the number of milliseconds from a reference date. – Cooper Oct 03 '19 at 21:03
  • Remember, GAS is just an extension of JavaScript so, if your question is about something that is not specific to Google Apps, you will probably find a thorough answer by searching in a JavaScript context. Here's a very thorough answer in a JavaScript context: https://stackoverflow.com/questions/492994/compare-two-dates-with-javascript – zimady Oct 04 '19 at 11:20
  • Possible duplicate of [Compare two dates with JavaScript](https://stackoverflow.com/questions/492994/compare-two-dates-with-javascript) – zimady Oct 04 '19 at 11:22

1 Answers1

1

You can get timestamp im ms from Date object and compare it:

var d1 = new Date(2019, 9, 6).getTime();
var d2 = new Date(2019, 9, 7).getTime();

var isD1Bigger = d1 > d2;
MR.QUESTION
  • 359
  • 2
  • 9