2

I have a Jquery UI datepicker on my site, but when i choose minDate and maxDate it adds a month to each variable, why is that and how do i fix it?

var startDate = new Date(2014, 2, 24);
var endDate = new Date(2014, 3, 24);

FIDDLE

chriz
  • 1,339
  • 2
  • 16
  • 32
Legarndary
  • 957
  • 2
  • 16
  • 37
  • 1
    possible duplicate of [javascript is creating date wrong month](http://stackoverflow.com/questions/12254333/javascript-is-creating-date-wrong-month) – Álvaro González Feb 25 '14 at 12:55

3 Answers3

3

In javascript, month count start from 0 and ends to 11 like:

0-Jan
1-Feb
2-Mar
3-Apr
4-May
5-Jun
6-Jul
7-Aug
8-Sep
9-Oct
10-Nov
11-Dec
Mohit Pandey
  • 3,679
  • 7
  • 26
  • 38
2

The month is zero-based, so add 1 to your chosen dates.

e.g.

var yourDate = new Date(2014, 3, 24); //would be 24 April 2014
DAC84
  • 1,254
  • 1
  • 20
  • 30
1

If you date is taken from the database the easiest way to do so

var startDate = new Date(2014, 2, 24);
var endDate = new Date(2014, 3, 24);

startDate.setMonth(startDate.getMonth()-1);
endDate.setMonth(endDate.getMonth()-1);
mixalbl4
  • 3,507
  • 1
  • 30
  • 44