I want to create two dates that contain the beginning date of a month and the beginning date of the following month. This is what I have:
var TheMonth = "6.2012"; // as in june 2012
TheMonth = TheMonth.split(".")
var TheDisplayDate = new Date(parseInt(TheMonth[1], 10), (parseInt(TheMonth[0], 10) - 1), 1);
var TheUpperLimit = new Date(TheDisplayDate.getFullYear(), TheDisplayDate.getMonth(), 1);
//I'm adding a month here but it's not changing the month
TheUpperLimit.setMonth(TheUpperLimit.getUTCMonth() + 1);
The problem is that TheUpperLimit turns out to be the same date. I have a fiddle here to look at.
What am I doing wrong?
Thanks.
The problem is that you use
getUTCMonthwithsetMonthinstead ofsetUTCMonth. It’s more correct to use only UTC or only non-UTC methods together. When you usesetMonthandgetMonth, it seems to work correct. But when you use UTC variations of these functions, it gives you 2 jul instead of 1 jul (in my time zone, at least). I suggest you to use the following code to avoid complications with time: