Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set jQuery datepicker format to dd-mm-year

I have the following in my view:

<input type="text" name="Model.Date" value="@Model.DateOfAction.ToShortDateString()"  class="txtDate" readonly = "readonly" style = "width: 90px; padding: 0px;" />

I then have below jQuery:

$(document).ready(function() {
    $(document.getElementsByClassName(".txtDate")).each(function () {
        $(this).datepicker({
            dateFormat: 'dd-mm-yy',
            showButtonPanel: true,
            changeMonth: true,
            changeYear: true,
            defaultDate: new Date(),
        });
    });
});

But "18th March 2016" is coming up as "3/18/2016" and I was expecting this "18-03-2016"

How do I properly set my desired format of "18-03-2016" (day,month,year)?. Notice that I want the separator to be this - not /.

like image 312
StackTrace Avatar asked Dec 13 '25 02:12

StackTrace


1 Answers

You have not set the date format correctly. Here is a working fiddle. Basically you have to set the dateFormat like below:

$(function() {
    $('.txtDate').datepicker({
            dateFormat: 'dd-mm-yy',
            showButtonPanel: true,
            changeMonth: true,
            changeYear: true,
            defaultDate: new Date()
        });
  });
like image 92
Deepak Biswal Avatar answered Dec 15 '25 17:12

Deepak Biswal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!