jQuery jquery日期选择器从包含日期时间的字符串中将日期格式设置为'MM-DD-YYYY'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18359599/
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 date picker set date format as 'MM-DD-YYYY' from a string containing datetime
提问by Paul Phoenix
I am trying to set the jquery datepicker date format but it is not working, I have read few posts and answer already but none of them worked for me. Below is the code I am using please check and tell me where I am doing wrong. I am getting the datetime from Database as 2012-03-06 00:00:00 UTC
我正在尝试设置 jquery datepicker 日期格式,但它不起作用,我已经阅读了一些帖子并回答了但没有一个对我有用。下面是我正在使用的代码,请检查并告诉我哪里做错了。我从数据库中获取日期时间为2012-03-06 00:00:00 UTC
<script>
$(document).ready(function() {
$(".datepicker").datepicker({
dateFormat:'MM-DD-YYYY'
}).val();
});
</script>
Also I tried
我也试过
<script>
$(document).ready(function() {
var dateTypeVar = $('.datepicker').datepicker('getDate');
$.datepicker.formatDate('dd-mm-yy', dateTypeVar);
});
</script>
回答by Irvin Dominin
This 2012-03-06 00:00:00 UTC
is not a valid JavaScript date, so the datepicker
can't accept the value assigned.
这2012-03-06 00:00:00 UTC
不是有效的 JavaScript 日期,因此datepicker
不能接受分配的值。
Date
object: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
Date
对象:https: //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
setDate
method: http://api.jqueryui.com/datepicker/#method-setDate
setDate
方法:http: //api.jqueryui.com/datepicker/#method-setDate
Get the date in a compliant format, and then set the datepicker
in this way.
以兼容格式获取日期,然后datepicker
以这种方式设置。
Code:
代码:
$(document).ready(function () {
var dbDate = "2012-03-06";
var date2 = new Date(dbDate);
$(".datepicker").datepicker({
dateFormat: 'mm-dd-yy'
}).datepicker('setDate', date2)
});
回答by Gregory Dias
$(document).ready(function () {
var date2 = new Date().getDate()-9;
$(".datepicker").datepicker({
dateFormat: 'dd-mm-yy'
}).datepicker('setDate', date2)
});