2016-09-18 79 views
0

我有我的看法如下代码:jQuery的日期选择不接受的日期格式

<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线,它们都返回准确输入(毫米/日/年)

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()); 
+1

当你说'输入的默认格式'是否意味着你没有使用日期选择器输入日期?这几乎听起来像你正在试图使用日期选择器来转换日期格式,这不是这意味着做什么。你能扩展一点吗? – tristansokol

+0

我正在使用日期选择器为用户选择'从'和'日期'到'的日期。我想比较用户选择的日期到mysql表中的日期。当我做一个警报($('#date-from')。datepicker('getDate'));返回值为星期五9月30日00:00:00 GMT + 0200(浪漫夏令时间) – Dennis

回答

0

下面是在yyyy-mm-dd输出是否帮助一个datepicker的例子?

$(function() { 
 
    $('#datepicker').datepicker({ 
 
    onSelect: function(date) { 
 
     alert(date); 
 
    }, 
 
    dateFormat: 'yyyy-mm-dd', 
 
    }); 
 
});
<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>

0

呦可以使用Alt-格式http://api.jqueryui.com/datepicker/#option-altFormat 。然后在您的ajax调用中,您可以读取具有所需格式日期值的隐藏字段值表单。

$(function(){ 
 
    
 
    $('#datepicker').datepicker({ 
 
     dateFormat: 'dd-mm-yy', 
 
     altField: '#altdate', 
 
     altFormat: 'yy-mm-dd' 
 
    }); 
 
    
 
});
<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" />

或者你也可以手动使用做到这一点的分割线的基础上, “ - ” 和扭转它

$(function() { 
 
    $("#datepicker").datepicker({ 
 
    dateFormat: "dd-mm-yy", 
 
    }); 
 
    $("#datepicker").change(function() { 
 
    var currentDate = $("#datepicker").val(); 
 
    console.log("Desired format : ",currentDate.split("-").reverse().join('-')); 
 
    }); 
 
});
<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">