Jquery datepicker format date not working

后端 未结 12 2108
我在风中等你
我在风中等你 2021-01-01 22:23

Here is my code:

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

And the datetime picker is shown, but

相关标签:
12条回答
  • 2021-01-01 22:53

    try out this

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

    small letter will works

    0 讨论(0)
  • 2021-01-01 22:58

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

    $('#datepicker').datepicker({ format: 'yyyy-mm-dd' });
    
    0 讨论(0)
  • 2021-01-01 23:04

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

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

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

    The fix is you have to set the dateFormat option manually after the datepicker initialization:

    $(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');
    });
    
    0 讨论(0)
  • 2021-01-01 23:05

    Try just format option not dateFormat

    $('#datepicker').datepicker({
       format: 'dd-mm-yyyy' 
    });
    
    0 讨论(0)
  • 2021-01-01 23:07

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

    try:

    dateFormat: 'dd-mm-yy'
    

    (small letters)

    0 讨论(0)
  • 2021-01-01 23:07

    See this Jsfiddle link that is working example for me:

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

    0 讨论(0)
提交回复
热议问题