比较两个日期Google应用脚本

Jac*_*vdb 16 datetime date date-manipulation google-apps-script

比较两个日期的最佳方法是什么?

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){ ....
Run Code Online (Sandbox Code Playgroud)

我看到.toString()选项,但似乎只适用于==或===运算符.

这个问题有什么明确的吗?

meg*_*024 26

Date对象具有valueOf返回自1970-01-01午夜以来的毫秒数的方法.您可以使用它来比较日期.就像是

var date01 = new Date();
var date02 = new Date(2012, 5, 24);
if (date01.valueOf() > date02.valueOf()) {
   ....
}
Run Code Online (Sandbox Code Playgroud)


小智 14

有人发布了这一段时间,我觉得它非常有帮助

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();
    }
}
Run Code Online (Sandbox Code Playgroud)


cma*_*uro 5

// 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);
}
Run Code Online (Sandbox Code Playgroud)


ZAR*_*ZAR 5

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"));
Run Code Online (Sandbox Code Playgroud)

如果您要从电子表格中获取一个值,只需将该值放入新的 Date() 中:

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


Ser*_*sas 4

日期对象可以像任何其他变量一样进行比较。唯一棘手的事情是,例如,如果您需要比较同一天的两个日期并期望得到 date A = date B ,在这种情况下您就会遇到问题,因为日期还包括小时和分钟(以及秒 + 毫秒)!(这就是为什么我建议使用字符串来检查您引用的帖子中的相等性)。您可以做的是将两个变量中的小时、分钟、秒和毫秒设置为 0,以便仅在日、月、年进行比较。请参阅w3schools 日期参考页面以了解如何执行此操作。

另一种可能性是将两个日期都转换为字符串Utilities.formatDate()并使用来substrings()获取您需要的数据,但我想这不是一个非常优雅的方法;-)