2

我使用 date.js 与以用户友好的字符串形式编写的日期进行比较(星期六,2006 年 7 月 1 日 12:34:14)。我使用此代码。

function is_new(lasttime, newtime){
    lasttime = Date.parse(lastime);
    newtime = Date.parse(newtime);
    if(lasttime.isBefore(newtime)){
        return true;
    }else{
        return false;
    }
}

lasttime 和 newtime 都是像上面这样的字符串。当我尝试这个时,我得到

Uncaught TypeError: Object Tue Dec 30 1997 00:00:00 GMT+0000 (GMT Standard Time) has no method 'isBefore'
4

2 回答 2

1

没有名为 isBefore 的函数。相反,比较时间戳:

function is_new(LastTime,NewTime){
  return new Date(LastTime).getTime()<new Date(NewTime).getTime();
}

alert(is_new("Dec 30, 1997","Dec 31, 1997"));
于 2010-06-19T19:26:50.547 回答
1

实际上在较新的版本中有这个功能。只需在http://www.datejs.com/查看源代码或在http://www.datejs.com/build/date.js下载 date.js 文件。它对我有用。

于 2013-04-28T18:07:06.967 回答