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

标签 javascript jquery html css

我使用过 Jquery 日期选择器。但我需要将日期格式从 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, ''];
}
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;
}
<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>

最佳答案

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

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, ''];
}
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;
}
<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>

关于javascript - 我如何在 Jquery UI datepicker 中更改日期格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42947549/

相关文章:

javascript - 获取knockoutJs中选定的值

javascript - JQuery - 如何将 li 移动到 ul 中的另一个位置? (交换2里)

javascript - 如何使用 &lt;input type ="file"/> 的文件名更改 <label> 中的文本

javascript - Owl Carousel 中堆叠的图像

javascript - 如果您使用 SBT 0.10 或更高版本,如何缩小 Javascript

javascript - 有没有办法在 Office.js Excel 插件中更改线条形状的颜色?

javascript - 多个点击监听器被添加到同一个 html 元素中

javascript - 如何生成 1 到 100 万之间的随机数?

javascript - 填写表单中的所有输入后按钮未启用

html - 无法插入背景图片 html/css