JQuery Datepicker 日期格式 - 如何从 dd/mm/yyyy 更改为 dd/mm/yy?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44412688/
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
JQuery Datepicker date format - How to change from dd/mm/yyyy to dd/mm/yy?
提问by Ayushi Zile
I have used JQuery Datepicker where the date format is dd/mm/yyyy. I would like the date to be displayed as dd/mm/yy.
我使用了 JQuery Datepicker,其中日期格式为 dd/mm/yyyy。我希望日期显示为 dd/mm/yy。
Eg. Instead of 15/07/2017, I would like the date to be shown as 15/07/17 in the datepicker.
例如。我希望日期在日期选择器中显示为 15/07/17,而不是 15/07/2017。
This is how I am calling the JQuery datepicker, but it is not formating the date as per my need.
这就是我调用 JQuery 日期选择器的方式,但它没有根据我的需要格式化日期。
$( ".startdate" ).datepicker({
dateFormat: 'dd-mm-yy',
changeMonth: true,
changeYear: true
});
Is there any inbuilt option for this date format? Or is there a way to create a custom date format according to our own needs.
这种日期格式有任何内置选项吗?或者有没有办法根据我们自己的需要创建自定义日期格式。
回答by Anant Singh---Alive to Die
You need to do like below:-
您需要执行以下操作:-
$( ".startdate" ).datepicker({
dateFormat: 'dd/mm/y',//check change
changeMonth: true,
changeYear: true
});
Example:-
例子:-
$( ".startdate" ).datepicker({
dateFormat: 'dd/mm/y',//check change
changeMonth: true,
changeYear: true
});
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha/js/bootstrap.js" data-modules="effect effect-bounce effect-blind effect-bounce effect-clip effect-drop effect-fold effect-slide"></script>
<link rel="stylesheet" href="https://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css" />
<h1>Datepicker with Date Format (dd/mm/y) using JQuery</h1>
<p>Date picker: <input type="text" class="startdate" size="30"/></p>
<br>
Date Format : dd/mm/y
回答by FentomX1
This is what worked for me in every datePicker version (similar approach as in PHP)
这在每个 datePicker 版本中都对我有用(与 PHP 中的方法类似)
$.datepicker.formatDate("dd/mm/y", $.datepicker.parseDate('dd/mm/yy', lowestDate))