开发者

Javascript - Problem comparing dates

The code below is giving me trouble. When I select July 1st 11:00pm for fromDate, result 3 is being set to Thu Jun 02 2011 11:52:26. I check the values of fromDate and toDate in chrome and they seem fine but result 3 is getting set to a wacky date. Any solutions?

function(value, element, params) {
            fromDate = new Date(startDate.val());
            toDate = new Date(endDate.val());
            result1 = this.optional(element);
            result2 = fromDate <= toDate;
            result3 = new Date();
  开发者_C百科          result3.setDate(fromDate.getDate()+1);
            result5 = (toDate.getDate()+0);
            result4 = (fromDate.getDate()+1)>(toDate.getDate()+0);
            return result1 || (result2 && result4);
        }


result3.setDate(fromDate.getDate()+1) only sets the day of the month of your Date object. Since result3 creates a new Date object, this means that result3 is not having its time set correctly.

If you want to set result3 to fromDate plus one day, you'll have to do something like this:

var DAY_IN_MILLISECONDS = 1000 * 60 * 60 * 24;
result3 = new Date(fromDate + DAY_IN_MILLISECONDS);

However, since I'm not sure what your variables represent, it's hard to diagnose.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜