添加月份选择的datapicker jquery

时间:2017-11-21 20:41:12

标签: javascript jquery

您好我想从选定的开始日期(inizio)添加1个月,因为我需要发送一个表格,其中罚款(结束)日期的最短日期是所选日期的日期,对不起英语,任何人都有帮助?

<script type="text/javascript" src="piano_formativo.js"></script>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script >
  function dataodierna(){
    var today = new Date();
    var dd = today.getDate();
    var mm = today.getMonth()+1; //January is 0!
    var yyyy = today.getFullYear();
    today= yyyy+'/'+mm+'/'+dd;
    return today;
    }
  $(function() {
        $( "#inizio" ).datepicker({
          minDate: new Date(dataodierna()),
          dateFormat: 'yy/mm/dd',
          changeMonth: true,
          changeYear: true,
          onSelect: function(selectedDate) {
                $('#fine').datepicker('option', 'minDate', selectedDate);
          }
        });
        $( "#fine" ).datepicker({
          minDate: '+1m',
          dateFormat: 'yy/mm/dd',
          changeMonth: true,
          changeYear: true,

        });
  });

1 个答案:

答案 0 :(得分:0)

如果我理解正确,您想将“精确”日期输入的最小日期设置为在“inizio”输入中选择的日期再加上1个月? 然后你可以简单地做:

$(function() {
    $( "#inizio" ).datepicker({
      minDate: new Date(),
      dateFormat: 'yy/mm/dd',
      changeMonth: true,
      changeYear: true,
      onSelect: function(selectedDate) {
        var plusMonthDate = new Date(selectedDate);
        plusMonthDate.setMonth(plusMonthDate.getMonth()+1)
        $('#fine').datepicker('option', 'minDate', plusMonthDate);
      }
    });
    $( "#fine" ).datepicker({
      minDate: '+1m',
      dateFormat: 'yy/mm/dd',
      changeMonth: true,
      changeYear: true,
    });
});

此外,您不需要执行minDate: new Date(dataodierna()),因为new Date()本身已经返回今天的日期。

如果要在“inizio”中选择的日期后一个月将“罚款”日期设置为完全,您可以执行以下操作:

$(function() {
   $( "#inizio" ).datepicker({
      minDate: new Date(),
      dateFormat: 'yy/mm/dd',
      changeMonth: true,
      changeYear: true,
      onSelect: function(selectedDate) {
        var plusMonthDate = new Date(selectedDate);
        plusMonthDate.setMonth(plusMonthDate.getMonth()+1)
        $('#fine').datepicker('option', 'minDate', plusMonthDate);
        $('#fine').datepicker('option', 'maxDate', plusMonthDate);
        $('#fine').datepicker('setDate', plusMonthDate);
      }
    });
    $( "#fine" ).datepicker({
      minDate: '+1m',
      dateFormat: 'yy/mm/dd',
      changeMonth: true,
      changeYear: true,
    });
});

希望它有所帮助!