Scott Selby
Scott Selby

Reputation: 9580

next weekend's datetime in javascript returning invalid date

right now this code works for tomorrow and next month - weekend use to work , except now that it is approaching the end of the month it is causing an error. my startdate is set to 8/31/12 and enddate is 8/34/12 (which is obivously going to throw an error when converted into DateTime in Sql.)

I am successfully doing math to add a day , and to add a month , but when it comes to adding days into the next month it breaks. I think I understand why this is hapening , it is getting date - "29" turning it to into int then adding - I suppose I will get the same problem for "next month" in December.

My question is what is the proper way (or any working way) to add days to javascript date, where it understands the days of the month.

        var d = new Date();
        var startdate;
        var enddate;

         if ($('._tomorrow').attr("checked") == "checked") {
                startdate = [d.getMonth() + 1, d.getDate() + 1, d.getFullYear()].join('/');
                enddate = [d.getMonth() + 1, d.getDate() + 2, d.getFullYear()].join('/');
            }
            if ($('._weekend').attr("checked") == "checked") {
                startdate = [d.getMonth() + 1, d.getDate() + 6 - d.getDay(), d.getFullYear()].join('/');
                enddate = [d.getMonth() + 1, ( d.getDate() + 6 - d.getDay()) + 2, d.getFullYear()].join('/');
            }
            if ($('._nextmonth').attr("checked") == "checked") {
                startdate = [d.getMonth() + 2, 1, d.getFullYear()].join('/');
                enddate = [d.getMonth() + 2, 29 , d.getFullYear()].join('/');
            }

Upvotes: 2

Views: 484

Answers (2)

Alnitak
Alnitak

Reputation: 339917

To add a specified number of days, create a new Date object and then use d.setDate(d.getDate() + n) where n is the required number of days to add.

If you then call getYear(), getMonth() and getDate() again you will find that they've all been "corrected" to valid values:

> d = new Date()
Wed Aug 29 2012 20:57:12 GMT+0100 (BST)
> [d.getDate(), d.getMonth() + 1]
[29, 8]
> d.setDate(d.getDate() + 10);
1347134232993
> [d.getDate(), d.getMonth() + 1]
[8, 9]

Adding months is not so straightforward, because you need to define what happens when the following month is shorter than the current month - e.g. what date is "one month after 31st January" ?

Calculating the start of the next month is easy enough, though!

var now = new Date();
var start = new Date(now.getFullYear(), now.getMonth(), now.getDate());
var end   = new Date(now.getFullYear(), now.getMonth(), now.getDate());

if ($('._tomorrow').is(':checked')) {
    start.setDate(now.getDate() + 1);
      end.setDate(now.getDate() + 2);
} else if ($('._weekend').is(':checked')) {
    start.setDate(now.getDate() + 6 - now.getDay());
      end.setDate(now.getDate() + 8 - now.getDay());
} else if ($('._nextmonth').is(':checked')) {
    start.setDate(1);
      end.setDate(1);
    start.setMonth(now.getMonth() + 1);
      end.setMonth(now.getMonth() + 2);
}

var startStr = [start.getMonth() + 1, start.getDate(), start.getFullYear()].join('/');
var endStr = [end.getMonth() + 1, end.getDate(), end.getFullYear()].join('/');

See http://jsfiddle.net/alnitak/nTSMg/

Upvotes: 0

Marc B
Marc B

Reputation: 360762

You need to build a new date object with the adjusted values in them. Right now you're simply taking some randomish numbers and increasing them, e.g.

31+3/8/2012 -> 34/8/2012, not 3/9/2012

Do something like this first:

d.setMonth(d.getMonth()+ 2)

Internally JS will properly adjust the day/month/years to account for the "overflow" you cause at the end of each period, and still produce a valid date.

Upvotes: 2

Related Questions