我有两个数据采集器,minDate从第二个数据传输器是日期从第一个数据。问题是,如果我想将dateFormat变量中的值从'mm/dd/yy‘更改为’dd‘,那么第二个数据报警器就会丢失minDate。我希望在这两个数据显示的日期格式是‘dd-mm’。
$(function() {
  var dateFormat = 'mm/dd/yy',
    from = $("#from")
    .datepicker({
      minDate: 0,
      numberOfMonths: 2
    })
    .on("change", function() {
      to.datepicker("option", "minDate", getDate(this));
    }),
    to = $("#to").datepicker({
      dateFormat: 'dd-mm-yy',
      numberOfMonths: 2
    })
  function getDate(element) {
    var date;
    try {
      date = $.datepicker.parseDate(dateFormat, element.value);
    } catch (error) {
      date = null;
    }
    return date;
  }
});<link href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<input id="from" />
<input id="to" />
发布于 2019-06-23 23:27:06
在您提供的代码中,并不是所有地方都使用相同的日期格式。
$(function() {
  var dateFormat = 'dd-mm-yy',
    from = $("#from")
    .datepicker({
      minDate: 0,
      numberOfMonths: 2,
      dateFormat: dateFormat,
    })
    .on("change", function() {
      to.datepicker("option", "minDate", getDate(this));
    }),
    to = $("#to").datepicker({
      dateFormat: dateFormat,
      numberOfMonths: 2
    })
  function getDate(element) {
    var date;
    try {
      date = $.datepicker.parseDate(dateFormat, element.value);
    } catch (error) {
      date = null;
    }
    return date;
  }
});在这里,我将变量dateFormat提供给修复它的数据编辑器。
https://stackoverflow.com/questions/56726799
复制相似问题