Javascript 在 Datepicker 中将日期格式更改为 yyyy-mm-dd
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37124277/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change the date format to yyyy-mm-dd in Datepicker
提问by learntogrow-growtolearn
I am using Datepicker for my HTML field . The HTML code is :
我在 HTML 字段中使用 Datepicker。HTML代码是:
<input type="text" id="from-datepicker"/>
The Jquery code to set and get the date picker fields is :
设置和获取日期选择器字段的 Jquery 代码是:
$("#from-datepicker").datepicker({ dateFormat: 'yy-mm-dd'});
$("#from-datepicker").on("change", function () {
var fromdate = $(this).val();
alert(fromdate);
});
This code, however, displays the field in 'mm-dd-yyyy'
format.
Can someone tell me where am I going wrong ?
但是,此代码以'mm-dd-yyyy'
格式显示该字段。有人能告诉我我哪里错了吗?
My imported CDNs are :
我导入的 CDN 是:
<script type="text/javascript"
src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.6.0/js/bootstrap-datepicker.min.js"></script>
<link rel="stylesheet" type="text/css"
href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.6.1/css/bootstrap-datepicker3.min.css">
Thanks in advance.
提前致谢。
回答by jasonlam604
Yes, change dateFormat to just format and you need 4 sets of 'y's
是的,将 dateFormat 更改为仅格式化,您需要 4 组“y”
Sample code:
示例代码:
<script src="https://code.jquery.com/jquery-2.2.3.min.js" integrity="sha256-a23g1Nt4dtEYOj7bR+vTu7+T8VP13humZFBJNIYoEJo=" crossorigin="anonymous"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.6.0/js/bootstrap-datepicker.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.6.1/css/bootstrap-datepicker3.min.css">
<script>
$( document ).ready(function() {
$("#from-datepicker").datepicker({
format: 'yyyy-mm-dd'
});
$("#from-datepicker").on("change", function () {
var fromdate = $(this).val();
alert(fromdate);
});
});
</script>
<input type="text" id="from-datepicker"/>
回答by BrOrlandi
See the format
option: http://bootstrap-datepicker.readthedocs.io/en/latest/options.html#format
查看format
选项:http: //bootstrap-datepicker.readthedocs.io/en/latest/options.html#format
toDisplay: function (date, format, language) to convert date object to string, that will be stored in input field
toValue: function (date, format, language) to convert string object to date, that will be used in date selection
toDisplay:函数(日期,格式,语言)将日期对象转换为字符串,将存储在输入字段中
toValue:函数(日期,格式,语言)将字符串对象转换为日期,将用于日期选择
回答by Yosvel Quintero Arguelles
The option is format
选项是 format
You can set a default value for all your app:
您可以为所有应用设置默认值:
$.fn.datepicker.defaults.format = 'yy-mm-dd';
Or:
或者:
$("#from-datepicker").datepicker({format: 'yy-mm-dd'});
回答by LinuxUser
UPDATE 2018
2018 年更新
As of bootstrap-datepicker version 1.6.4, setting data-date-format
attribute to 'yyyy-mm-dd' on an input element works as well.
从 bootstrap-datepicker 版本 1.6.4 开始,data-date-format
在输入元素上将属性设置为 'yyyy-mm-dd' 也有效。
For e.g.
例如
<input data-provide="datepicker" name="date_selected" data-date-format="yyyy-mm-dd" placeholder="Select date" required>