jquery 日期选择器的默认值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28604379/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 16:29:53  来源:igfitidea点击:

Default value for jquery date picker

jqueryjquery-ui

提问by mohsinali1317

I have some questions regarding jQuery UI date picker.

我有一些关于 jQuery UI 日期选择器的问题。

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.

1 - 有没有默认值的选项?我想要做的是用今天的日期预填充我的输入文本类型。

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.

2 - 如果我将 type = "Date" 而不是 type = "Text" 然后在其上使用 jquery 时间选择器,则在选择日期时会出现此错误。

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

How do I solve this?

我该如何解决这个问题?

采纳答案by Sadikhasan

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

创建日期选择器并设置日期。您可以通过“new Date()”指定今天的日期。您可以设置输入type=texttype=date.

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

回答by void

(function() {

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

})();

回答by Butterfly

Refer Below one: It works fine for me.

参考以下之一:它对我来说很好用。

Jquery UI date-picker Set default date

Jquery UI date-picker 设置默认日期

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