jquery datepicker不接受dateformat

时间:2016-09-18 15:45:23

标签: javascript jquery date datepicker

我在视图中有以下代码:

<input type="text" class="form-control" id="date-from" name="date-from" value="" placeholder="Van" data-class="datepicker" />

我想通过AJAX检索此字段的值以进行过滤。 输入的默认格式是mm / dd / yyyy(我无法改变...)

所以我尝试以yyyy-mm-dd格式获得结果,因为我想要它们。

以下是我在javascript中的行,它们都返回了确切的输入(mm / dd / yyyy)

alert($('#date-from').datepicker({ format: 'yy-mm-dd' }).val());
            alert($('#date-from').datepicker({ format: 'yyyy-mm-dd' }).val());
            alert($('#date-from').datepicker({ dateFormat: 'yy-mm-dd' }).val());
            alert($('#date-from').datepicker({ dateFormat: 'yyyy-mm-dd' }).val());

2 个答案:

答案 0 :(得分:0)

以下是yyyy-mm-dd中输出的日期选择器示例有帮助吗?

&#13;
&#13;
$(function() {
  $('#datepicker').datepicker({
    onSelect: function(date) {
      alert(date);
    },
    dateFormat: 'yyyy-mm-dd',
  });
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.0/themes/smoothness/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.0/jquery-ui.min.js"></script>
<div id="datepicker"></div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

哟可以使用alt格式http://api.jqueryui.com/datepicker/#option-altFormat 。然后在您的ajax调用中,您可以读取值格式隐藏字段,其中包含所需格式的日期值。

&#13;
&#13;
$(function(){
    
    $('#datepicker').datepicker({
        dateFormat: 'dd-mm-yy',
        altField: '#altdate',
        altFormat: 'yy-mm-dd'
    });
    
});
&#13;
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/smoothness/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="//code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
Date Field : <input id="datepicker" type="text" /><br />
Alt Hidden Field : <input id="altdate" type="text" /><br />
<input id="thesubmit" type="button" value="Submit" />
&#13;
&#13;
&#13;

或者您可以通过在&#34; - &#34;的基础上拆分字符串来手动使用。反之亦然

&#13;
&#13;
$(function () {
  $("#datepicker").datepicker({
    dateFormat: "dd-mm-yy",
  });
  $("#datepicker").change(function () {
    var currentDate = $("#datepicker").val();
    console.log("Desired format : ",currentDate.split("-").reverse().join('-'));
  });
});
&#13;
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/smoothness/jquery-ui.css">
       <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
        <script src="//code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<input id="datepicker" type="text">
&#13;
&#13;
&#13;

相关问题