Comparing date part only without comparing time in JavaScript

后端 未结 22 1415
春和景丽
春和景丽 2020-11-22 10:59

What is wrong with the code below?

Maybe it would be simpler to just compare date and not time. I am not sure how to do this either, and I searched, but I couldn\'t

相关标签:
22条回答
  • 2020-11-22 11:50

    Comparing with setHours() will be a solution. Sample:

    var d1 = new Date();
    var d2 = new Date("2019-2-23");
    if(d1.setHours(0,0,0,0) == d2.setHours(0,0,0,0)){
        console.log(true)
    }else{
        console.log(false)
    }
    
    0 讨论(0)
  • 2020-11-22 11:52

    Using Moment.js

    If you have the option of including a third-party library, it's definitely worth taking a look at Moment.js. It makes working with Date and DateTime much, much easier.

    For example, seeing if one Date comes after another Date but excluding their times, you would do something like this:

    var date1 = new Date(2016,9,20,12,0,0); // October 20, 2016 12:00:00
    var date2 = new Date(2016,9,20,12,1,0); // October 20, 2016 12:01:00
    
    // Comparison including time.
    moment(date2).isAfter(date1); // => true
    
    // Comparison excluding time.
    moment(date2).isAfter(date1, 'day'); // => false
    

    The second parameter you pass into isAfter is the precision to do the comparison and can be any of year, month, week, day, hour, minute or second.

    0 讨论(0)
  • 2020-11-22 11:53

    This might be a little cleaner version, also note that you should always use a radix when using 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);
        }
    });
    

    Checkout the MDC parseInt page for more information about the radix.

    JSLint is a great tool for catching things like a missing radix and many other things that can cause obscure and hard to debug errors. It forces you to use better coding standards so you avoid future headaches. I use it on every JavaScript project I code.

    0 讨论(0)
  • 2020-11-22 11:54

    Just use toDateString() on both dates. toDateString doesn't include the time, so for 2 times on the same date, the values will be equal, as demonstrated below.

    var d1 = new Date(2019,01,01,1,20)
    var d2 = new Date(2019,01,01,2,20)
    console.log(d1==d2) // false
    console.log(d1.toDateString() == d2.toDateString()) // true
    

    Obviously some of the timezone concerns expressed elsewhere on this question are valid, but in many scenarios, those are not relevant.

    0 讨论(0)
  • 2020-11-22 11:54

    You can use fp_incr(0). Which sets the timezone part to midnight and returns a date object.

    0 讨论(0)
  • 2020-11-22 11:55

    I know this question have been already answered and this may not be the best way, but in my scenario its working perfectly, so I thought it may help someone like me.

    if you have date string as

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

    and you just want to compare the date part with another Date object in JS,

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

    then you have to convert the string to Date object by using new Date("2018-01-01T18:19:12.543");

    and here is the trick :-

    var valueDate =new Date(new Date(dateString).toDateString());
    
                return valueDate.valueOf() == anotherDate.valueOf(); //here is the final result
    

    I have used toDateString() of Date object of JS, which returns the Date string only.

    Note: Don't forget to use the .valueOf() function while comparing the dates.

    more info about .valeOf() is here reference

    Happy codding.

    0 讨论(0)
提交回复
热议问题