24

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?

1

13 Answers 13

34

Try just format option not dateFormat

$('#datepicker').datepicker({
   format: 'dd-mm-yyyy' 
});
Sign up to request clarification or add additional context in comments.

4 Comments

you saved me also. Cheers!
This is working. Try just format option not dateFormat. Because due to different version of datepicker.
Thanks, dateFormat: 'dd-mm-yy' is not working in my code. Format is working
Thanks. Everywhere, it is shown as "dateformat" in examples.
15

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

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

Comments

14

try out this

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

small letter will works

Comments

12

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

try:

dateFormat: 'dd-mm-yy'

(small letters)

4 Comments

It's confusing but "yy" actually gives you the 4-digit year.
Depends which datepicker, and which distribution of it. Different ones use different meanings for upper and lower case y, and d. Everyone wants to re-invent everything for their "special" version, instead of sticking with the strftime standard. And is it "format" or dateFormat? Just keep piddling and fiddling until you "discover" what the particular author thought was the "cooler way" to do it.
@JosephK totally that! The API docs are very clear that the property is "dateFormat", but after reading your comment I tried "format" and it started working. Oh, and for extra fun, I needed to use "yyyy" for a four-digit date. Yeah, consistency FTW :-/
If you find "format" and using "yyyy" instead of "yy" is working for you, you may actually be using the bootstrap version of datepicker. Be sure to check your script src included .js files to see WHOSE datepicker you're using.
10

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

Comments

3

See this Jsfiddle link that is working example for me:

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

Comments

3

Full example:

In 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);
    }
   }
  );
}

Comments

3

this one works for me

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

Comments

3

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) {
            ...
        }
    });
});

enter image description here

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.

enter image description here

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');
});

Comments

2

Try this:

$(function() {
    $('#datepicker').datepicker({
        dateFormat: 'dd/mm/yy',
    }).datepicker('option', 'dateFormat', 'dd/mm/yy');
});

It works 100%.

1 Comment

its really help when we have date range picker and datepicker-ui both in one screen.
2
$('.datepicker').datepicker({
        format: 'dd-mm-yyyy',
        autoclose: true
    })

1 Comment

While this code may answer the question, providing some additional context regarding how and why it solves the problem would improve the answer's long-term value.
1

I had similar issue: dateFormat was set in defaults, but it did not work.

$.App.Utils.DatePicker = {
  initialize: function(event) {
    $.datepicker.setDefaults({
      dateFormat: 'dd.mm.yy',
      firstDay: 1,
      changeMonth: true,
      changeYear: true,
      showOtherMonths: true,
      selectOtherMonths: true,
      showOn: 'both',
      buttonImage: '/assets/calendar.png',
      buttonImageOnly: true
    })
  },

activate: function(event) {
    this.initialize()
    $('input.datepicker').datepicker()
  }

But date was still displayed in default format, with slashes, like 02/03/2021. Maybe this option can not be overwritten in defaults.

Working solution: remove dateFormat from setDefaults and set it when initializing datepicker:

activate: function(event) {
    this.initialize()
    $('input.datepicker').datepicker({ dateFormat: 'dd.mm.yy' })
}

Docs

Comments

0

C#:-

 [Column(TypeName = "date")]
        [Display(Name = "Date of Birth")]
        [Required]
        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
        public DateTime? DOB { get; set; }

CSHTL:-

 @Html.EditorFor(model => model.DOB, new { htmlAttributes = new { @class = "form-control" , @required = "required", @autocomplete="off"} })

        $(document).ready(function () {
        var validationerror = false;
         $('input[type=date]').datepicker({
             dateFormat: "yy-mm-dd",
             changeMonth: true,
             changeYear: true,
             yearRange: "-60:+0"
         });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.