1

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:

function val_date(input){
    var date = new Date(input);
    date = date.getTime() / 1000;
    var timestamp = new Date().getTime() + (7 * 24 * 60 * 60 * 1000)
    window.alert("Date: "+date + " = N_Date: "+timestamp);
    if(timestamp > date || timestamp === date){
        // The selected time is less than 7 days from now
        return false;
    }
    else if(timestamp < date){
    // The selected time is more than 7 days from now
        return true;
    }
    else{
    // -Exact- same timestamps.
        return false;
    }
}

I am using an alert so that I can check my progress to make sure the dates are different. The output of the alert just says:

Date: NaN = N_Date = 13255772630 (<- or something like that).

Is there something I am doing wrong here?
Not sure if it helps but my date format is DD-MM-YYYY

CSchulz
  • 10,882
  • 11
  • 60
  • 114
Paul Ledger
  • 1,125
  • 4
  • 21
  • 46

6 Answers6

4

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.

RobG
  • 142,382
  • 31
  • 172
  • 209
3

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;
    }
}
Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107
  • The OP's string format is DD-MM-YYYY which will not be correctly parsed by most browser Date constructors. Date strings should be manually parsed. The created date will be at 0:00:00 but *dateWeek* will be at the current time so will return false on the 7th day when it should return true. – RobG Nov 23 '13 at 13:10
0

Using moment.js:

moment([2013, 2, 29]).fromNow();
Mark Simpson
  • 2,344
  • 2
  • 23
  • 31
0

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.

SnapShot
  • 5,464
  • 5
  • 42
  • 40
0

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);
    }
}
mipmap
  • 221
  • 1
  • 13
0

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;
 }
Praveen
  • 55,303
  • 33
  • 133
  • 164
  • *input* is a string that will not be correctly parsed by the Date constructor (as noted in the OP). Date objects **can** be directly compared. – RobG Nov 23 '13 at 13:16
  • @RobG comparing date objects didn't worked check this http://jsfiddle.net/g9KGR/ Corrct me if meant wrong – Praveen Nov 23 '13 at 13:21
  • @RobG `var date = new Date("12-12-2013");` is working only in chrome but fails in other browsers. I missed to note this before, so I posted the answer. – Praveen Nov 23 '13 at 13:27
  • 1
    Ok, you can't use the `==` operator because it uses the [abstract equality algorithm](http://ecma-international.org/ecma-262/5.1/#sec-11.9.3), but you can use operators like `>` and `<` because they use the [abstract relational comparison](http://ecma-international.org/ecma-262/5.1/#sec-11.8.5) that coerces the operands to number first. – RobG Nov 23 '13 at 13:32
  • @RobG Thank you, for taking your time for this explanation. Still lot to learn :) – Praveen Nov 23 '13 at 13:39