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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 21:26:16  来源:igfitidea点击:

jquery date picker set date format as 'MM-DD-YYYY' from a string containing datetime

jqueryjquery-uijquery-plugins

提问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 UTCis not a valid JavaScript date, so the datepickercan't accept the value assigned.

2012-03-06 00:00:00 UTC不是有效的 JavaScript 日期,因此datepicker不能接受分配的值。

Dateobject: 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

setDatemethod: 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 datepickerin 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)

});

Demo: http://jsfiddle.net/IrvinDominin/7ck7D/

演示:http: //jsfiddle.net/IrvinDominin/7ck7D/

回答by Gregory Dias

$(document).ready(function () {

    var date2 = new Date().getDate()-9;

    $(".datepicker").datepicker({
        dateFormat: 'dd-mm-yy'
    }).datepicker('setDate', date2)

});

http://jsfiddle.net/IrvinDominin/7ck7D/

http://jsfiddle.net/IrvinDominin/7ck7D/