Borje
Borje

Reputation: 255

jquery datepicker change date format

I'm trying to change date format as the year is changed. The default format is "dd M" but when a date not in the current year is selected then I want to add the year. This all works well adding year to the format. It's when changing back I run into trouble.

$("#id").datepicker({
    ...
    onSelect: function(dateText, inst){
        if (currentYear == currentDate.getFullYear()) {
            $("#id").datepicker("option", "dateFormat", "dd M");
        } else {
            $("#id").datepicker("option", "dateFormat", "dd M yy");
        }
});

It seems as though the datepicker really doesn't like going from having the year included in the format to removing it.

Anyone got any suggestions?

Upvotes: 0

Views: 616

Answers (1)

Jsparo30
Jsparo30

Reputation: 405

There are some formats which datepicker accepts them only Like

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


$(".datepicker").datepicker({dateFormat: 'd M, y'});

You can check other formats in

https://jqueryui.com/datepicker/#date-formats

Upvotes: 1

Related Questions