Javascript compare two dates to get a difference

后端 未结 6 2086
长情又很酷
长情又很酷 2020-12-12 04:24

I am trying to compare two different dates to see if the date inputted is after 7 days of todays date. I have done a bit of googling and come up with this:

f         


        
相关标签:
6条回答
  • 2020-12-12 04:49

    Date: NaN Because string which you are passing to date creation is not possible to create Date


    Try

    fiddle Demo

    Date.prototype.addDays = function (days) {
        this.setDate(this.getDate() + days);
        return this;
    };
    
    function val_date(input) {
        var inputDate = new Date(input);
        var dateWeek = new Date().addDays(7);
        console.log(inputDate, dateWeek);
        if (inputDate < dateWeek) {
            // The selected time is less than 7 days from now
            return false;
        } else {
            // The selected time is more than 7 days from now
            return true;
        }
    }
    
    0 讨论(0)
  • 2020-12-12 05:01

    This is pretty common and if you will be manipulating dates you might want to use a Javascript library for this. There is an excellent on called moment.js

    Using this you would do something like:

    moment().add('days', 7)

    To find a week in the future.

    0 讨论(0)
  • 2020-12-12 05:10

    Using moment.js:

    moment([2013, 2, 29]).fromNow();
    
    0 讨论(0)
  • 2020-12-12 05:11

    Try like this

    var date = new Date(input).getTime(); // Get the milliseconds
    var today = new Date();    
    
    //You can't compare date, 
    //so convert them to milliseconds
    today = new Date(today.setDate(today.getDate() + 7)).getTime(); 
    if (inputDate < today) {
        // The selected time is less than 7 days from now
        return false;
     } else if{ ()
        // -Exact- same timestamps.
        return false;
     }
     else {
        // The selected time is more than 7 days from now
        return true;
     }
    
    0 讨论(0)
  • 2020-12-12 05:12

    Assuming that input is a valid Javascript Date object, you could perhaps try:

    function dateDifference(oldDate) {
        var currentDate = new Date();
        var difference = currentDate - oldDate; //unit: milliseconds
        var numDays = 7;
        var threshHoldTime = numDays * (24 * 60 * 60 * 1000); //seven days in milliseconds
        if (difference > threshHoldTime ) {
            console.log("The difference is greateer then then 7 days");
        }
        else {
            console.log("the date is not enough: " + difference);
        }
    }
    
    0 讨论(0)
  • 2020-12-12 05:15

    If you are comparing dates and don't want to include time, you can use something like:

    // dateString is format DD-MM-YYYY
    function isMoreThan7DaysHence(dateString) {
    
        // Turn string into a date object at 00:00:00
        var t = dateString.split('-');
        var d0 = new Date(t[2], --t[1], t[0]);
    
        // Create a date for 7 days hence at 00:00:00
        var d1 = new Date();
        d1.setHours(0, 0, 0, 0);
        d1.setDate(d1.getDate() + 7);
    
        return d0 >= d1;
    }
    

    Note that the hours for today's date must be zeroed.

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