Google Apps Script:
Comparing two dates

How to:

In Google Apps Script, dates are compared using JavaScript Date objects, enabling the use of standard methods to evaluate which of two dates is earlier, later, or if they are the same. Here’s a basic approach:

function compareDates() {
  var date1 = new Date('2023-04-01T00:00:00');
  var date2 = new Date('2023-04-15T00:00:00');

  // Compare dates
  if (date1 < date2) {
    Logger.log('Date1 is before Date2');
  } else if (date1 > date2) {
    Logger.log('Date1 is after Date2');
  } else {
    Logger.log('Both dates are the same');
  }
}

// Sample output:
// Date1 is before Date2

For more detailed comparisons (like the number of days between two dates), you can subtract one date from another, which returns the difference in milliseconds:

function daysBetweenDates() {
  var date1 = new Date('2023-04-01');
  var date2 = new Date('2023-04-15');
  
  var difference = date2 - date1;
  
  var days = difference / (1000 * 60 * 60 * 24); // Convert milliseconds to days
  Logger.log(days + ' days between dates');
}

// Sample output:
// 14 days between dates

Deep Dive

Google Apps Script leverages the core principles of JavaScript Date objects for date comparison, which has been a fundamental aspect of the language since its inception. The use of milliseconds as a comparative value since the Unix Epoch (January 1, 1970) provides a high level of precision for determining differences or similarities between dates.

While this approach is effective for most use cases within the scope of Google Apps Script, it’s worth noting that operations on dates — like timezone corrections and leap year calculations — can sometimes lead to confusion. Developers from other programming backgrounds (like Python, where datetime and dateutil modules provide a more nuanced handling of dates) might find the JavaScript Date object to be lacking in features.

For complex date handling and manipulations beyond simple comparisons, libraries such as Moment.js (which can still be used within Google Apps Script through external APIs) offer a rich set of functionalities that address these shortcomings. However, the native JavaScript Date object continues to serve as a reliable tool for most date comparison tasks, particularly in the context of Google Apps Script and its integration with Google’s suite of applications.