Question

I have searched this forum and found many useful answers, but one of the answers that I used only works under certain conditions.

I am populating a week calendar, and simply need to determine the start of the week (Monday) from a Date picker, and then I add to that date to populate text fields with the following 6 days. This works only if the date picker selection is in the same month.

So, if I select Wednesday May 15th 2013, it correctly returns and populates the Monday with May 13, the Tuesday with May 14, etc.

But, if I select Wednesday May 1, 2013, it correctly populates Monday Apr 29, but Tuesday it puts as May 30 (adding a month instead of a day).

I should note that I am building this in Application Craft, so I don't know if that has any impact.

Here's my code:

var curr = new Date(app.getValue("DatePicker2")); // get selected date
var first = curr.getDate() - curr.getDay() +1; // Adjust for monday start of week
var firstday = new Date(curr.setDate(first));
var secondday = new Date();
secondday.setDate(firstday.getDate()+1);

Can anyone see where I have gone wrong?

Thanks

Tammy

Was it helpful?

Solution

Here

secondday.setDate(firstday.getDate()+1)

since you are specifying only the date in the setDate function, it would assume "this" month which happens to be May in this case

So, you can do

secondday = new Date(firstday.getFullYear(), firstday.getMonth(), firstday.getDate()+1)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top