Default value for jquery date picker

2020-07-10 11:10发布

I have some questions regarding jQuery UI date picker.

1 - Is there an option to have a default value? What I am trying to do is pre-filled my input text type with today's date.

2 - If I put the type = "Date" instead of type = "Text" and then use jquery time picker on it, I get this error when I select the date.

 The specified value '02/18/2015' does not conform to the required format, 'yyyy-MM-dd.'

How do I solve this?

3条回答
狗以群分
2楼-- · 2020-07-10 11:44
(function() {

  $(".dp").datepicker({
    format: 'dd-mm-yyyy',
    startDate: new Date(),
    endDate: ''
  }).on("show", function() {
    $(this).val(new Date()).datepicker('update');
  });

})();
查看更多
乱世女痞
3楼-- · 2020-07-10 11:47

To create the date picker and set the date. You can assign today's date by 'new Date().' You can set input type=text or type=date.

$('.datepicker').datepicker({ 
     dateFormat: 'dd-mm-yy'
     }).datepicker("setDate", new Date());
查看更多
老娘就宠你
4楼-- · 2020-07-10 12:07

Refer Below one: It works fine for me.

Jquery UI date-picker Set default date

<script>
    $(function() {
        $( "#datepicker" ).datepicker({
            dateFormat: 'dd-mm-yy'
        }).val(getTodaysDate(0)); // For current date

        $( "#datepicker2" ).datepicker({
            dateFormat: 'dd-mm-yy',
            }).val(getTodaysDate(1));  // For previous month's date
    });

  function getTodaysDate (val) {
    var t = new Date, day, month, year = t.getFullYear();
    if (t.getDate() < 10) {
        day = "0" + t.getDate();
    }
    else {
        day = t.getDate();
    }
    if ((t.getMonth() + 1) < 10) {
        month = "0" + (t.getMonth() + 1 - val);
    }
    else {
        month = t.getMonth() + 1 - val;
    }

    return (day + '/' + month + '/' + year);
   }
</script>
查看更多
登录 后发表回答