下面的代码有什么问题?

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

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

我的代码:

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);
    }
});

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


当前回答

这可能是一个更简洁的版本,还请注意,在使用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项目中都使用它。

其他回答

我知道这个问题已经有人回答了,这可能不是最好的方法,但在我的情况下,它工作得很好,所以我想它可能会帮助像我这样的人。

如果你有日期字符串为

String dateString="2018-01-01T18:19:12.543";

你只是想将date部分与JS中的另一个date对象进行比较,

var anotherDate=new Date(); //some date

然后你必须使用new Date("2018-01-01T18:19:12.543")将字符串转换为Date对象;

诀窍在这里:-

var valueDate =new Date(new Date(dateString).toDateString());

            return valueDate.valueOf() == anotherDate.valueOf(); //here is the final result

我已经使用了JS的Date对象的toDateString(),它只返回日期字符串。

注意:不要忘记在比较日期时使用. valueof()函数。

关于.valeOf()的更多信息在这里参考

快乐的鳕鱼。

您可以使用fp_incr(0)。它将时区部分设置为午夜,并返回一个日期对象。

这对我来说很管用:

 export default (chosenDate) => {
  const now = new Date();
  const today = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate()));
  const splitChosenDate = chosenDate.split('/');

  today.setHours(0, 0, 0, 0);
  const fromDate = today.getTime();
  const toDate = new Date(splitChosenDate[2], splitChosenDate[1] - 1, splitChosenDate[0]).getTime();

  return toDate < fromDate;
};

在已接受的答案中,有时区问题,其他时间不是00:00:00

在看到这个问题的同时,我决定发布另一个解决方案,因为我觉得它不太令人满意,至少对我的需求来说:

我曾经用过这样的东西:

var currentDate= new Date().setHours(0,0,0,0);

var startDay = new Date(currentDate - 86400000 * 2);
var finalDay = new Date(currentDate + 86400000 * 2);

这样,我就可以用我想要的格式来处理这些日期。但这只是为了我的需要,但我还是决定张贴它,也许它会帮助到别人

因为我在这里没有看到类似的方法,而且我不喜欢将h/m/s/ms设置为0,因为它会导致在更改日期对象时精确过渡到本地时区的问题(我假设是这样),让我在这里介绍一下这个,几分钟前写的,lil函数:

+:易于使用,使一个基本的比较操作完成(比较日,月和年没有时间。) -:这似乎与“开箱即用”的思维完全相反。

function datecompare(date1, sign, date2) {
    var day1 = date1.getDate();
    var mon1 = date1.getMonth();
    var year1 = date1.getFullYear();
    var day2 = date2.getDate();
    var mon2 = date2.getMonth();
    var year2 = date2.getFullYear();
    if (sign === '===') {
        if (day1 === day2 && mon1 === mon2 && year1 === year2) return true;
        else return false;
    }
    else if (sign === '>') {
        if (year1 > year2) return true;
        else if (year1 === year2 && mon1 > mon2) return true;
        else if (year1 === year2 && mon1 === mon2 && day1 > day2) return true;
        else return false;
    }    
}

用法:

Datecompare (date1, '===', date2) Datecompare (date1, '>', date2) !datecompare(date1, '>', date2)进行较少或相等的检查

显然,您还可以在某些地方切换date1和date2,以实现任何其他简单的比较。