Datepicker setData在dateFormat'MM yy'上显示错误的日期

时间:2016-07-19 08:47:31

标签: javascript jquery datepicker

我正在使用一个只需要显示和选择月份和年份的日期选择器。所以我将dateFormat改为'MM yy'。现在我做的时候:

$('.date-picker').datepicker('setDate', 'July 2016');

datepicker显示2022年1月。但是当我这样做时:

console.log($('.date-picker').datepicker("getDate"));

显示2016年7月19日星期二00:00:00 GMT + 0800(PHT)

我尝试将dateFormat更改为'MM dd yy'并将setDate更改为'2016年7月1日',它运行顺畅。

这是JSFiddle:http://jsfiddle.net/gaddygs3/3/

谢谢!

2 个答案:

答案 0 :(得分:0)

$(document).ready(function() {
$('#txtDate').datepicker({
 changeMonth: true,
 changeYear: true,
 dateFormat: 'MM yy',

 onClose: function() {
    var iMonth = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
    var iYear = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
    $(this).datepicker('setDate', new Date(iYear, iMonth, 1));
 },

 beforeShow: function() {
   if ((selDate = $(this).val()).length > 0) 
   {
      iYear = selDate.substring(selDate.length - 4, selDate.length);
      iMonth = jQuery.inArray(selDate.substring(0, selDate.length - 5), $(this).datepicker('option', 'monthNames'));
      $(this).datepicker('option', 'defaultDate', new Date(iYear, iMonth, 1));
       $(this).datepicker('setDate', new Date(iYear, iMonth, 1));
   }
}
});
});

Check here

答案 1 :(得分:-1)

首先更改设置日期的格式

$('#checkin').datepicker('setDate',new Date('July 2016'));