-2

I'm following Daniel's answer here : Compare two dates with JavaScript

But it's not working as is should. This is my code:

selectedDateEnd is a date selected from jQuery UI Datepicker
controller is the a date from a list (last date added (in the future))
12096e5 is a magic number which is 14 days in milliseconds.

A user can only create a new item that is 14 days older than the last item created.

    if(+selectedDateEnd >= (+controller + 12096e5)){
        cl('selectedDateEnd >= controller');
        cl(selectedDateEnd + ' >= ' + controller);
    } else {
        cl('selectedDateEnd < controller');
        cl(selectedDateEnd + ' < ' + controller);
    }

And the output is this:

selectedDateEnd < controller
Thu Feb 12 2015 00:00:00 GMT+0100 < Thu Feb 12 2015 00:00:00 GMT+0100

According to the logic, it should output:

    Thu Feb 12 2015 00:00:00 GMT+0100 >= Thu Feb 12 2015 00:00:00 GMT+0100

What have I done wrong here? See my fiddle: http://jsfiddle.net/ueoxv9w7/

Community
  • 1
  • 1
Steven
  • 19,224
  • 47
  • 152
  • 257

1 Answers1

2

You're only adding the magic number in the if condition, and not when you print out the dates.

Do this:

//12096e5 is a magic number which is 14 days in milliseconds.
controller.setTime(+controller + 12096e5);
if(+selectedDateEnd >= +controller){
    cl('selectedDateEnd >= controller');
    cl(selectedDateEnd + ' >= ' + controller);
} else {
    cl('selectedDateEnd < controller');
    cl(selectedDateEnd + ' < ' + controller);
}
JLRishe
  • 99,490
  • 19
  • 131
  • 169