Skip to content

Commit 1359517

Browse files
committed
fixed bug pull/220 where nextMonth was not being calculated properly
1 parent f95856e commit 1359517

File tree

1 file changed

+51
-21
lines changed

1 file changed

+51
-21
lines changed

src/dateinput/dateinput.js

Lines changed: 51 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* Since: Mar 2010
1010
* Date: @DATE
1111
*/
12-
(function($) {
12+
(function($, undefined) {
1313

1414
/* TODO:
1515
preserve today highlighted
@@ -91,7 +91,7 @@
9191

9292
// @return amount of days in certain month
9393
function dayAm(year, month) {
94-
return 32 - new Date(year, month, 32).getDate();
94+
return new Date(year, month + 1, 0).getDate();
9595
}
9696

9797
function zeropad(val, len) {
@@ -339,10 +339,10 @@
339339

340340

341341
if (index > 41) {
342-
self.addMonth();
342+
showNextMonth();
343343
el = $("#" + css.weeks + " a:eq(" + (index-42) + ")");
344344
} else if (index < 0) {
345-
self.addMonth(-1);
345+
showPreviousMonth();
346346
el = $("#" + css.weeks + " a:eq(" + (index+42) + ")");
347347
} else {
348348
el = days.eq(index);
@@ -354,17 +354,17 @@
354354
}
355355

356356
// pageUp / pageDown
357-
if (key == 34) { return self.addMonth(); }
358-
if (key == 33) { return self.addMonth(-1); }
357+
if (key == 34) { return showNextMonth(); }
358+
if (key == 33) { return showPreviousMonth(); }
359359

360360
// home
361-
if (key == 36) { return self.today(); }
361+
if (key == 36) { return self.today(); }
362362

363363
// enter
364364
if (key == 13) {
365365
if (!$(e.target).is("select")) {
366-
$("." + css.focus).click();
367-
}
366+
$("." + css.focus).click();
367+
}
368368
}
369369

370370
return $([16, 17, 18, 9]).index(key) >= 0;
@@ -382,12 +382,42 @@
382382
});
383383
}
384384
//}}}
385-
385+
386+
387+
/**
388+
* @private
389+
*
390+
* Calculates the days in the next month to properly switch months
391+
*
392+
*/
393+
function showNextMonth() {
394+
395+
var daysNextMonth = dayAm(currYear, currMonth + 1);
396+
397+
/*
398+
* If next month has less days than the current date
399+
* add number of days in the next month, otherwise add
400+
* number of days in the current month
401+
*/
402+
return self.addDay(currDay > daysNextMonth ? daysNextMonth : dayAm(currYear, currMonth));
403+
}
404+
405+
/**
406+
* @private
407+
*
408+
* Return to previous month
409+
*/
410+
function showPreviousMonth() {
411+
return self.addDay(-dayAm(currYear, currMonth));
412+
}
386413

387414
$.extend(self, {
388415

389-
//{{{ show
390-
416+
417+
/**
418+
* @public
419+
* Show the calendar
420+
*/
391421
show: function(e) {
392422

393423
if (input.attr("readonly") || input.attr("disabled") || opened) { return; }
@@ -417,14 +447,14 @@
417447
// prev / next month
418448
pm = root.find("#" + css.prev).unbind("click").click(function(e) {
419449
if (!pm.hasClass(css.disabled)) {
420-
self.addMonth(-1);
450+
showPreviousMonth();
421451
}
422452
return false;
423453
});
424454

425455
nm = root.find("#" + css.next).unbind("click").click(function(e) {
426456
if (!nm.hasClass(css.disabled)) {
427-
self.addMonth();
457+
showNextMonth();
428458
}
429459
return false;
430460
});
@@ -456,16 +486,15 @@
456486

457487
return self;
458488
},
459-
//}}}
460-
461-
462-
//{{{ setValue
463489

490+
/**
491+
* @public
492+
*
493+
* Set the value of the dateinput
494+
*/
464495
setValue: function(year, month, day) {
465496

466-
467-
468-
var date = integer(month) >= -1 ? new Date(integer(year), integer(month), integer(day || 1)) :
497+
var date = integer(month) >= -1 ? new Date(integer(year), integer(month), integer(day == undefined || isNaN(day) ? 1 : day)) :
469498
year || value;
470499

471500
if (date < min) { date = min; }
@@ -495,6 +524,7 @@
495524

496525
currMonth = month;
497526
currYear = year;
527+
currDay = day;
498528

499529
// variables
500530
var tmp = new Date(year, month, 1 - conf.firstDay), begin = tmp.getDay(),

0 commit comments

Comments
 (0)