下面的代码有什么问题?

也许只比较日期而不是时间会更简单。我也不确定如何做到这一点,我搜索了一下,但我找不到我的确切问题。

顺便说一句,当我在警报中显示这两个日期时,它们显示完全相同。

我的代码:

window.addEvent('domready', function() {
    var now = new Date();
    var input = $('datum').getValue();
    var dateArray = input.split('/');
    var userMonth = parseInt(dateArray[1])-1;
    var userDate = new Date();
    userDate.setFullYear(dateArray[2], userMonth, dateArray[0], now.getHours(), now.getMinutes(), now.getSeconds(), now.getMilliseconds());

    if (userDate > now)
    {
        alert(now + '\n' + userDate);
    }
});

有没有一种更简单的方法来比较日期而不包括时间?


当前回答

这将有所帮助。我设法把它弄成这样。

var currentDate = new Date(new Date().getFullYear(), new Date().getMonth() , new Date().getDate())

其他回答

这可能是一个更简洁的版本,还请注意,在使用parseInt时应该始终使用基数。

window.addEvent('domready', function() {
    // Create a Date object set to midnight on today's date
    var today = new Date((new Date()).setHours(0, 0, 0, 0)),
    input = $('datum').getValue(),
    dateArray = input.split('/'),
    // Always specify a radix with parseInt(), setting the radix to 10 ensures that
    // the number is interpreted as a decimal.  It is particularly important with
    // dates, if the user had entered '09' for the month and you don't use a
    // radix '09' is interpreted as an octal number and parseInt would return 0, not 9!
    userMonth = parseInt(dateArray[1], 10) - 1,
    // Create a Date object set to midnight on the day the user specified
    userDate = new Date(dateArray[2], userMonth, dateArray[0], 0, 0, 0, 0);

    // Convert date objects to milliseconds and compare
    if(userDate.getTime() > today.getTime())
    {
            alert(today+'\n'+userDate);
    }
});

检查MDC parseInt页面以获得关于基数的更多信息。

JSLint是一个很好的工具,可以捕捉诸如缺少基数之类的东西,以及许多其他可能导致模糊和难以调试的错误的东西。它迫使您使用更好的编码标准,以避免将来的麻烦。我在编写的每个JavaScript项目中都使用它。

我还在学习JavaScript,我找到的唯一方法是比较两个没有时间的日期,使用Date对象的sehours方法,并将小时、分钟、秒和毫秒设置为零。然后比较这两个日期。

例如,

date1 = new Date()
date2 = new Date(2011,8,20)

Date2将小时、分钟、秒和毫秒设置为0,但date1将它们设置为date1创建的时间。要去掉date1上的小时、分钟、秒和毫秒,请执行以下步骤:

date1.setHours(0,0,0,0)

现在您可以将两个日期仅作为日期进行比较,而不必担心时间元素。

这个怎么样?

Date.prototype.withoutTime = function () {
    var d = new Date(this);
    d.setHours(0, 0, 0, 0);
    return d;
}

它允许你像这样比较日期的日期部分,而不影响变量的值:

var date1 = new Date(2014,1,1);
new Date().withoutTime() > date1.withoutTime(); // true

如果你真的是只比较日期而不比较时间组件,另一个解决方案可能感觉不对,但可以避免所有date()时间和时区问题,就是直接使用字符串比较来比较ISO字符串日期:

> "2019-04-22" <= "2019-04-23"
true
> "2019-04-22" <= "2019-04-22"
true
> "2019-04-22" <= "2019-04-21"
false
> "2019-04-22" === "2019-04-22"
true

你可以使用以下方法获取当前日期(UTC日期,不一定是用户的本地日期):

> new Date().toISOString().split("T")[0]
"2019-04-22"

我支持它的理由是程序员的简单性——与试图正确处理datetimes和偏移量相比,您不太可能搞砸它,可能是以速度为代价的(我没有比较性能)

var fromdate = new Date(MM/DD/YYYY);
var todate = new Date(MM/DD/YYYY);
if (fromdate > todate){
    console.log('False');
}else{
    console.log('True');
}

如果你的日期格式不同,那么使用moment.js库转换你的日期格式,然后使用上面的代码来比较两个日期

例子:

如果您的日期是“DD/MM/YYYY”,并希望将其转换为“MM/DD/YYYY”,请参阅下面的代码示例

var newfromdate = new Date(moment(fromdate, "DD/MM/YYYY").format("MM/DD/YYYY"));
console.log(newfromdate);
var newtodate = new Date(moment(todate, "DD/MM/YYYY").format("MM/DD/YYYY"));
console.log(newtodate);