下面的代码有什么问题?

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

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

我的代码:

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

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


当前回答

这个JS将在设置日期之后更改内容 这里有同样的东西,但在w3schools上

date1 = new Date() date2 = new Date(2019,5,2) //the date you are comparing date1.setHours(0,0,0,0) var stockcnt = document.getElementById('demo').innerHTML; if (date1 > date2){ document.getElementById('demo').innerHTML="yes"; //change if date is > set date (date2) }else{ document.getElementById('demo').innerHTML="hello"; //change if date is < set date (date2) } <p id="demo">hello</p> <!--What will be changed--> <!--if you check back in tomorrow, it will say yes instead of hello... or you could change the date... or change > to <-->

其他回答

照例。太少,太迟了。

现在不鼓励使用momentjs(他们说的,不是我说的),首选是dayjs。

可以使用dayjs的isSame。

https://day.js.org/docs/en/query/is-same

dayjs().isSame('2011-01-01', 'date')

你还可以使用其他一些单位进行比较: https://day.js.org/docs/en/manipulate/start-of#list-of-all-available-units

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

例如,

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

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

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

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

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

如果你真的是只比较日期而不比较时间组件,另一个解决方案可能感觉不对,但可以避免所有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和偏移量相比,您不太可能搞砸它,可能是以速度为代价的(我没有比较性能)

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

如果你有日期字符串为

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()的更多信息在这里参考

快乐的鳕鱼。