如何在Jquery UI datepicker中更改日期格式

时间:2017-03-22 09:33:25

标签: javascript jquery html css

我使用过Jquery datepicker。但我需要将日期格式从 dd-mm-yy 更改为 yy-mm-dd 。 (还使用日期突出显示功能)



var dates = ['03-03-2017', '03-10-2017', '03-25-2017'];

$('#datepicker').datepicker({
  dateFormat: 'dd-mm-yy',
  //defaultDate: new Date('03/10/2017'), // this line is for testing
  beforeShowDay: highlightDays
});

function highlightDays(date) {
  for (var i = 0; i < dates.length; i++) {
    if (new Date(dates[i]).toString() == date.toString()) {
      return [true, 'highlight'];
    }
  }
  return [true, ''];
}
&#13;
td.highlight>a {
  background: #E50104!important;
  color: #fff!important;
  pointer-events: none;
}

.ui-state-highlight,
.ui-widget-content .ui-state-highlight,
.ui-widget-header .ui-state-highlight {
  border: 1px solid #000;
}
&#13;
<link rel="stylesheet" type="text/css" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<p>Date: <input type="text" id="datepicker"></p>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:3)

很简单。只需将dateFormat: 'dd-mm-yy',更改为dateFormat: 'yy-mm-dd'

即可

&#13;
&#13;
var dates = ['03-03-2017', '03-10-2017', '03-25-2017'];

$('#datepicker').datepicker({
    dateFormat: 'yy-mm-dd',
    //defaultDate: new Date('03/10/2017'), // this line is for testing
    beforeShowDay: highlightDays
});

function highlightDays(date) {
    for (var i = 0; i < dates.length; i++) {
        if (new Date(dates[i]).toString() == date.toString()) {
            return [true, 'highlight'];
        }
    }
    return [true, ''];
}
&#13;
td.highlight > a {
	background: #E50104!important;
	color: #fff!important;
  pointer-events: none;
}
.ui-state-highlight, .ui-widget-content .ui-state-highlight, .ui-widget-header .ui-state-highlight {
    border: 1px solid #000;
}
&#13;
<link rel="stylesheet" type="text/css" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css">
  <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
  
  <p>Date: <input type="text" id="datepicker"></p>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

答案是:

OverflowException