My question is. Why this bug only happen when I choose CheckIn_Date in August and September?
I have a project using ASP.NET MVC 3 and using jQuery for date picker. Lets say that I have 3 textboxes named CheckIn_Date, CheckOut_Date (this two using datepicker) and Days in my view.
When i set Days = 3 then I choose CheckIn_Date = 2012-01-01, my CheckOut_Date will automatically count the days and set the value to 2012-01-03.
All goes as I expect, until I choose 2012-08-01 in my CheckIn_Date and set the Days = 3. My CheckOut_Date value is not 2012-08-04 but 2012-12-04. This only happens when I choose my CheckIn_Date in August and September. Choosing any other month give me what I expect for my CheckOut_Date.
Here is my code:
$("input[data-datepicker='true']").change(function () {
var id = $(this).attr("name");
var days = parseInt($("#maxProgramDays").val());
//var o = new Date();
var val = $(this).val();
var s = val.split("-");
var x = [];
var thisDate = new Date();
thisDate.setFullYear(parseInt(s[0]), parseInt(s[1]) - 1, parseInt(s[2]));
//alert(thisDate.toDateString())
if (id == "Reservation[0].CheckIn_Date") {
thisDate.setDate(thisDate.getDate() + days);
x['year'] = thisDate.getFullYear();
x['month'] = m2d(thisDate.getMonth() + 1);
x['day'] = m2d(thisDate.getDate());
$("input[name='Reservation[0].CheckOut_Date']").val(x['year'] + '-' + x['month'] + '-' + x['day']);
//alert(thisDate.toDateString())
}
if (id == "Reservation[0].CheckOut_Date") {
thisDate.setDate(thisDate.getDate() - days);
x['year'] = thisDate.getFullYear();
x['month'] = m2d(thisDate.getMonth() + 1);
x['day'] = m2d(thisDate.getDate());
$("input[name='Reservation[0].CheckIn_Date']").val(x['year'] + '-' + x['month'] + '-' + x['day']);
//alert(thisDate.toDateString())
}
Here is my View:
<div class="display-label">Program Days</div>
<div class="display-field"><span id="pDays"></span></div>
<div class="editor-label">@Html.LabelFor(model => model.Reservation[0].CheckIn_Date)</div>
<div class="editor-field reserveDate">
@Html.Datepicker("Reservation[0].CheckIn_Date", "")
@Html.ValidationMessageFor(model => model.Reservation[0].CheckIn_Date)
</div>
<div class="editor-label">@Html.LabelFor(model => model.Reservation[0].CheckOut_Date)</div>
<div class="editor-field reserveDate">
@Html.Datepicker("Reservation[0].CheckOut_Date", "")
@Html.ValidationMessageFor(model => model.Reservation[0].CheckOut_Date)
</div>
The problem was out of this world. Here it goes when you give date as 2010-08-01, in your
thisDate.setFullYear(parseInt(s[0]), parseInt(s[1]) - 1, parseInt(s[2]));function,parseInt(s[1])at runtime look likesparseInt("08"), and here it goes wrongReason
How to fix it
So your code will become
This will work for you.