- Posted: 31st May 2011
- Created by: Robert
- Found in: jQuery
var arrivalDate = new Date();
arrivalDate.setFullYear( $('#arrival-year').val() );
arrivalDate.setMonth( $('#arrival-month').val() -1 );
arrivalDate.setDate( $('#arrival-day').val() );
So in the example of this code today is 31 May 2011 and that is declared as the arrivalDate to begin with. Say I was setting the arrival date to 1 August 2011 in the order of the code:
- Starts as: 31 May 2011
- Sets the year: 2011 => 31 May 2011
- Sets the month: August => 31 August 2011
Here is the problem: 31 August 2011 ??? Says the computer, that doesn't exists silly. There are only 30 days in August, I'm going to give you a valid date I can think of.
So in order to fix, I tried setting the date before the month, which in theory should work, but was proving buggy still. In the end my final code ended up declaring the date in the object initialisation. As follows:
var arrivalDate = new Date( $('#arrival-year').val(), $('#arrival-month').val() -1, $('#arrival-day').val() );
Hope this helps someone!