Jquery datepicker 格式日期不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9888000/
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-27 11:25:30  来源:igfitidea点击:

Jquery datepicker format date not working

jquerydatepicker

提问by petko_stankoski

Here is my code:

这是我的代码:

$(function () {
    $("#datepicker").datepicker({ dateFormat: 'DD-MM-YY' });
});

And the datetime picker is shown, but in format mm/dd/yyyy. Why is it not working?

并显示日期时间选择器,但格式为 mm/dd/yyyy。为什么它不起作用?

采纳答案by Kashiftufail

try out this

试试这个

$(".datepicker").datepicker({dateFormat: 'dd-mm-yy'});

small letter will works

小写字母会起作用

回答by Steen

Thats the default, meaning it does not recognise your option.

这是默认设置,这意味着它无法识别您的选项。

try:

尝试:

dateFormat: 'dd-mm-yy'

(small letters)

(小写字母)

回答by shuadoc

Depending on the version of datepicker being used, the correct format may be:

根据所使用的日期选择器的版本,正确的格式可能是:

$('#datepicker').datepicker({ format: 'yyyy-mm-dd' });

回答by Mayur Kambariya

Try just formatoption not dateFormat

尝试只格式化选项而不是dateFormat

$('#datepicker').datepicker({
   format: 'dd-mm-yyyy' 
});

回答by user2814778

Setting the default format will solve the problem, this solution works for me while all above do not.

设置默认格式将解决问题,此解决方案适用于我,而上述所有方法均无效。

$.datepicker.setDefaults({
     dateFormat: 'yy-mm-dd'
});

Source: Datepicker: Adding dateFormat is giving an error

来源:Datepicker:添加 dateFormat 出现错误

回答by Rony SP

See this Jsfiddle link that is working example for me:

请参阅此 Jsfiddle 链接,该链接对我有用:

http://jsfiddle.net/nEGTv/3/

http://jsfiddle.net/nEGTv/3/

回答by Rachman Kushinryu

this one works for me

这个对我有用

$('#datepicker').datepicker({ format: 'dd-mm-yyyy' });

回答by David Liang

If you're getting the default date value from your backend framework/service, i.e., Asp.Net MVC, setting the dateFormatwhen you initiate the datepickeron your input won't format the date initially.

如果您从后端框架/服务获取默认日期值,即Asp.Net MVC,设置dateFormatdatepicker在输入中启动的时间不会最初格式化日期。

$(function() {
    $('#date-start').datepicker({
        dateFormat: 'mm/dd/yy',
        onSelect: function(startDate) {
            ...
        }
    });
});

enter image description here

在此处输入图片说明

The screenshot above shows, even I am initializing the input with a short date like 03/15/2018, the datepickerwon't pick up the format initially. All selections afterward would work as expected.

上面显示的截图,甚至我初始化一个短日期,如输入03/15/2018时,datepicker最初不会拿起格式。之后的所有选择都会按预期工作。

enter image description here

在此处输入图片说明

The fix is you have to set the dateFormatoption manually after the datepickerinitialization:

解决方法是您必须dateFormatdatepicker初始化后手动设置该选项:

$(function() {
    // $('#date-start').datepicker({
    //     dateFormat: 'mm/dd/yy',
    //     onSelect: function(startDate) {
    //         ...
    //     }
    // });
    // $('#date-start').datepicker('option', 'dateFormat', 'mm/dd/yy');

    // Or you can chain them
    $('#date-start').datepicker({
        dateFormat: 'mm/dd/yy',
        onSelect: function(startDate) {
            ...
        }
    }).datepicker('option', 'dateFormat', 'mm/dd/yy');
});

回答by Sallyerik

Full example:

完整示例:

In jsp:

在jsp中:

<div id="datepicker"></div>

In script:

在脚本中:

function datepicker() {

  $("#datepicker").datepicker({
    showButtonPanel: true,
    dateFormat: 'yy-mm-dd',
    onSelect: function() {
        var dateObject = $('#datepicker').datepicker().val();
        alert(dateObject);
    }
   }
  );
}

回答by SHUBHASIS MAHATA

$('.datepicker').datepicker({
        format: 'dd-mm-yyyy',
        autoclose: true
    })