简体   繁体   English

复制时的jQuery DatePicker日期格式问题

[英]JQuery DatePicker Date Formatting issue when copying

Look to just copy the Year to the 2nd input - not the entire date. 看起来只将Year复制到第二个输入-而不是整个日期。 Here is the HTML so 1st input - date format is correct. 这是HTML,因此1st输入-日期格式正确。 2nd input - date format needs to be just the year and not the whole date. 第二个输入-日期格式必须仅是年份而不是整个日期。

look here http://jsfiddle.net/xyp7khfe/13/ 看看这里http://jsfiddle.net/xyp7khfe/13/

<input id="sdate" />
<input id="edate" />

and here is the JQUERY 这是JQUERY

$('#sdate').datepicker({
    dateFormat: 'dd-M-yy',
    changeMonth: false,
    changeYear: true,
    firstDay: 1,
    onSelect: function () {
        $('#edate').val(this.value);
    }
});

Instead of updating the value with the .val() method, you could use the setDate method to update the value while taking the dateFormat property value into consideration: 除了使用.val()方法更新值.val() ,还可以使用setDate方法来更新值,同时考虑dateFormat属性值:

$('#edate').datepicker('setDate', new Date(this.value));

Updated Example 更新示例

$('#sdate').datepicker({
    dateFormat: 'dd-M-yy',
    changeMonth: false,
    changeYear: true,
    firstDay: 1,
    onSelect: function () {
        $('#edate').datepicker('setDate', new Date(this.value));
    }
});

Alternatively, you can create a date based on the value and then use the .getFullYear() method : 或者,您可以基于该值创建日期,然后使用.getFullYear()方法

$('#edate').val(new Date(this.value).getFullYear());

Updated Example 更新示例

$('#sdate').datepicker({
  dateFormat: 'dd-M-yy',
  changeMonth: false,
  changeYear: true,
  firstDay: 1,
  onSelect: function() {
    $('#edate').val(new Date(this.value).getFullYear());
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM