2016-09-18 76 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">