如何用截止日期31-03显示结束日期

时间:2019-01-31 07:39:57

标签: javascript jquery jquery-ui jquery-ui-datepicker

我有两个日期选择器。在第一个日期选择器中是开始日期,在第二个日期选择器中是结束日期。

所以我在做的是,我的结束日期范围是31-03-year(depending upon the start date)

我选择开始日期31-01-2019,以便用户可以选择直到31-03-2019的结束日期。这种情况对我有用(请查看代码段)。

现在,我的问题是,如果用户选择开始日期01-04-2019,那么结束日期用户可以选择直到31-03-2020

所以我的约会日期是31-03。

我知道我必须使用类似下面的代码,但是我很困惑如何使用它。我应该使用If条件吗?

  var nextYear = $.datepicker.parseDate("dd-mm-yy", "31-03-" + (yy + 1));

您能在这个问题上帮助我吗?

$(function() {
  var year = (new Date).getFullYear();
  $(".start_date").datepicker({
    dateFormat: "dd-mm-yy",
    changeMonth: true,
    changeYear: true,
    minDate: 0,
    //maxDate    :  "+0Y",
    yearRange: new Date().getFullYear() + ':' + new Date().getFullYear(),
    showAnim: "clip",
    //numberOfMonths: 1,
    onSelect: function(dt, dp) {
      var selected = $.datepicker.parseDate("dd-mm-yy", dt);
      var yy = selected.getFullYear();
      var mm = selected.getMonth();
      var dd = selected.getDate();

      var nextYear = $.datepicker.parseDate("dd-mm-yy", "31-03-" + (yy));

      //alert(nextYear);
      $(".end_date").datepicker("option", "maxDate", nextYear);

    }
  });

  $(".end_date").datepicker({
    buttonText: "Select date",
    dateFormat: "dd-mm-yy",
    changeMonth: true,
    changeYear: true,
    minDate: 0,
    //maxDate    :  "+1Y",
    //maxDate: new Date(year, 03, 31),
    showAnim: "clip"
  });

});
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">

<div class="form-group">
  <label>Start Date</label>
  <input type="text" name="start_date" placeholder="Start Date" id="start_date" class="start_date form-control">
</div>
<div class="form-group">
  <label>End Date</label>
  <input type="text" name="end_date" id="end_date" placeholder="End Date" class="end_date form-control">
</div>
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.3.1.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

4 个答案:

答案 0 :(得分:1)

此功能将在输入日期之后的3月31日下找到您。之后,它应该很简单。

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var button: CustomButton!

    override func viewDidLoad() {
        super.viewDidLoad()

        button.viewController = self //Assigning controller using self keyword

    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(true)

    }

}

它在这里工作...

var myDate = new Date("2018-04-01")
myDate.setUTCMonth(Math.floor((myDate.getUTCMonth() + 10)/12)*12 + 2)
myDate.setUTCDate(31)
myDate.setUTCHours(0)
myDate.setUTCMinutes(0)
myDate.setUTCSeconds(0)
console.log(myDate)
$(function() {
var year = (new Date).getFullYear();
  $(".start_date").datepicker({
    dateFormat: "dd-mm-yy",
    changeMonth: true,
    changeYear: true,
    minDate: 0,
    //maxDate    :  "+0Y",
    yearRange: new Date().getFullYear() + ':' + new Date().getFullYear(),
    showAnim: "clip",
    //numberOfMonths: 1,
    onSelect: function(dt, dp) {
      var myDate = $.datepicker.parseDate("dd-mm-yy", dt);
      myDate.setUTCMonth(Math.floor((myDate.getUTCMonth() + 10)/12)*12 + 2)
      myDate.setUTCDate(31)
      myDate.setUTCHours(0)
      myDate.setUTCMinutes(0)
      myDate.setUTCSeconds(0)
      console.log(myDate)

      $(".end_date").datepicker("option", "maxDate", myDate);

    }
  });

  $(".end_date").datepicker({
    buttonText: "Select date",
    dateFormat: "dd-mm-yy",
    changeMonth: true,
    changeYear: true,
    minDate: 0,
    //maxDate    :  "+1Y",
    //maxDate: new Date(year, 03, 31),
    showAnim: "clip"
  });

});

答案 1 :(得分:1)

由于开始日期仅限于当年,目标日期是该月的最后一天,除了开始日期的月份外,您无需检查其他任何内容。

<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">

<div class="form-group">
  <label>Start Date</label>
  <input type="text" name="start_date" placeholder="Start Date" id="start_date" class="start_date form-control">
</div>
<div class="form-group">
  <label>End Date</label>
  <input type="text" name="end_date" id="end_date" placeholder="End Date" class="end_date form-control">
</div>
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.3.1.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

答案 2 :(得分:0)

这就是我要这样做的方式

$(function() {
  let today = new Date(Date.UTC());
  let currentYear = today.getUTCFullYear();
  $(".start_date").datepicker({
    dateFormat: "dd-mm-yy",
    changeMonth: true,
    changeYear: true,
    yearRange: currentYear+':'+currentYear,
    minDate: 0,
    showAnim: "clip",
    onSelect: function(date) {
      let selectedDate = new Date(date.split('-').reverse().join('-'));
      let selectedYear = selectedDate.getUTCFullYear();
      let cutoffDate = new Date(Date.UTC(selectedYear, 2, 31));
      let maxDate = selectedDate > cutoffDate ?
        new Date(Date.UTC(selectedYear+1, 2, 31)) :
        new Date(Date.UTC(selectedYear, 2, 31));
      $('.end_date').datepicker('option', 'minDate', date);
      $('.end_date').datepicker('option', 'maxDate', '31-03-'+maxDate.getUTCFullYear());
      $('.end_date').datepicker('option', 'yearRange', currentYear+':'+maxDate.getUTCFullYear());
    }
  });
  
  $(".end_date").datepicker({
    dateFormat: "dd-mm-yy",    
    changeMonth: true,
    changeYear: true,
    yearRange: currentYear+':'+currentYear,
    minDate: 0,
    showAnim: "clip"
  });
});
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">

<div class="form-group">
  <label>Start Date</label>
  <input type="text" name="start_date" placeholder="Start Date" id="start_date" class="start_date form-control">
</div>
<div class="form-group">
  <label>End Date</label>
  <input type="text" name="end_date" id="end_date" placeholder="End Date" class="end_date form-control">
</div>
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.3.1.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

答案 3 :(得分:0)

var year = (new Date).getFullYear();
$(".start_date").datepicker({
    dateFormat: "dd-mm-yy",
    changeMonth: true,
    changeYear: true,
    minDate: 0,
    //maxDate    :  "+0Y",
    yearRange: new Date().getFullYear() + ':' + new Date().getFullYear(),
    showAnim: "clip",
    //numberOfMonths: 1,
    onSelect: function (dt, dp) {
        var selected = $.datepicker.parseDate("dd-mm-yy", dt);
        var yy = selected.getFullYear();
        var mm = selected.getMonth();
        var dd = selected.getDate();

        if(mm > 2) {
          yy++;
        }
        else {
            yy;
        }
        var nextYear = $.datepicker.parseDate("dd-mm-yy", "31-03-" + (yy));
        $(".end_date").datepicker("option", "maxDate", nextYear);

    }
});

$(".end_date").datepicker({
    buttonText: "Select date",
    dateFormat: "dd-mm-yy",
    changeMonth: true,
    changeYear: true,
    minDate: 0,
    showAnim: "clip",
});