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

二次信任 提交于 2019-12-20 02:15:25

问题


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 /.


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/36078268/how-to-set-jquery-datepicker-format-to-dd-mm-year

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!