Compare two dates Google apps script

痴心易碎 提交于 2019-11-27 09:04:15

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()) {
   ....
}
JKWA

Somebody posted this a while back, I find it's very helpful

function testDate() {
    var futureDate = new Date('8/31/2020');
    var todayDate = new Date();
    Logger.log(DateDiff.inMonths(todayDate, futureDate));
    Logger.log(DateDiff.inYears(todayDate, futureDate));             
}

var DateDiff = {    
    inDays: function(d1, d2) {
        var t2 = d2.getTime();
        var t1 = d1.getTime();

        return parseInt((t2-t1)/(24*3600*1000));
    },
    inWeeks: function(d1, d2) {
        var t2 = d2.getTime();
        var t1 = d1.getTime();

        return parseInt((t2-t1)/(24*3600*1000*7));
    },
    inMonths: function(d1, d2) {
        var d1Y = d1.getFullYear();
        var d2Y = d2.getFullYear();
        var d1M = d1.getMonth();
        var d2M = d2.getMonth();

        return (d2M+12*d2Y)-(d1M+12*d1Y);
    },
    inYears: function(d1, d2) {
        return d2.getFullYear()-d1.getFullYear();
    }
}
// Date, Date -> Number
// Get the number of days between two dates

test("Date Diff In Days", 5, function(){
  ok(DateDiffInDays(new Date("January 1, 2000"), new Date("January 1, 2000")) == 0, "Ok");
  ok(DateDiffInDays(new Date("January 1, 2000"), new Date("January 2, 2000")) == 1, "Ok");
  ok(DateDiffInDays(new Date("January 1, 2000"), new Date("January 11, 2000")) == 10, "Ok");
  ok(DateDiffInDays(new Date("January 11, 2000"), new Date("January 1, 2000")) == -10, "Ok");
  ok(DateDiffInDays(new Date("January 1, 2000"), new Date("April 10, 2000")) == 100, "Ok");
});


function DateDiffInDays(a, b) 
{
  var _MS_PER_DAY = 1000 * 60 * 60 * 24;
  // Discard the time and time-zone information.
  var utc1 = Date.UTC(a.getFullYear(), a.getMonth(), a.getDate());
  var utc2 = Date.UTC(b.getFullYear(), b.getMonth(), b.getDate());
  return Math.floor((utc2 - utc1) / _MS_PER_DAY);
}

Wow, I'm late here. I found it easiest to convert the dates into the integer Day of the year. So Jan 1st would be 1, Feb 1st would be 32, etc.

Here is that script:

today = parseInt(Utilities.formatDate(new Date(),"EST","D"));

If you're grabbing a value from your spreadsheet, just place the value into the new Date():

today = parseInt(Utilities.formatDate(new Date(rSheet.getRange(parseInt(int) + 1 ,1).getValues()),"EST","D"));

Date objects can be compared just as any other variables. The only tricky thing is if you need to compare two dates on the same day for example and expect to get date A = date B , in this case you have a problem since dates also include hours and minutes (and second + milliseconds) ! (that's why I suggested to use strings to check equality in the post you refer to). What you could do is to set hours, minutes, seconds and milliseconds to 0 in both variables so the comparison occurs on day, month, year only. See w3schools date reference page to see how to do that.

Another possibility would be to convert both dates to strings using Utilities.formatDate() and play with substrings() to get the data you need but I guess this is not a very elegant way to proceed ;-)

Jacobvdb

I did a little work around, not so charming but it seems to serve.

var startDate = rSheet.getRange(parseInt(int) + 1 ,1).getValues();
var toDay = new Date();

 var sYyyy = Utilities.formatDate(new Date(startDate), "GMT-2","yyyy");
 var sMm = Utilities.formatDate(new Date(startDate), "GMT-2","MM");
 var sDd = Utilities.formatDate(new Date(startDate), "GMT-2","dd");

 var tYyyy = Utilities.formatDate(new Date(toDay), "GMT-2","yyyy");
 var tMm = Utilities.formatDate(new Date(toDay), "GMT-2","MM");
 var tDd = Utilities.formatDate(new Date(toDay), "GMT-2","dd");


 if (sYyyy + sMm + sDd > tYyyy + tMm + tDd) {....

I´ll check out the other answers as well and give it a spin.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!