Good morning gang.
I have a jQuery Datepicker object that is prepoulated using a javscript Date object, and I have getMonth() returning 50 as the month, instead of 5. This was previously working as recent as 4/27/2012.
Here’s the code:
$(document).ready(function(){
$('#datepicker1').attr('readonly', 'readonly');
var press_date = new Date(); //This is the Date object that you're getting from external source
$('#datepicker1').val(press_date.getFullYear()+'-'
+press_date.getMonth()+1+'-'
+press_date.getDate());
$('#datepicker1').datepicker({
showAnim:'slide',showOn: 'button',
dateFormat : 'yy-mm-dd',
buttonImage: 'images/calendar.jpg',
buttonImageOnly: true,onSelect: function(dateText, inst) {}
});
$('#datepicker').attr('readonly', 'readonly');
$('#datepicker').datepicker({
showAnim:'slide',showOn: 'button',
dateFormat : 'yy-mm-dd',
buttonImage: 'images/calendar.jpg',
buttonImageOnly: true,onSelect: function(dateText, inst) {}
});
});
My guess is that this is a simple issue to resolve, but I haven’t seen anything around the web to explain it.
The issue is the x.getMonth()+1 in your expression. Since you are concatenating strings the parser is getting confused. You can surround the x.getMonth()+1 in parenthesis or just move that calculation outside of the string concatenation:
For example, surround it with parenthesis:
x.getFullYear()+'-'+(x.getMonth()+1)+'-'+x.getDate()UPDATE: To get the month value zero padded I would definitively move the calculation to its own line to make it clear: