【问题标题】:How to get the number of days between two dates?如何获取两个日期之间的天数?
【发布时间】:2012-08-02 05:12:32
【问题描述】:

我需要计算两个日期之间的天数。我已经检查了这个链接给出的代码 How to calculate the number of days between two dates using JavaScript?.

在此示例中,如果输入 2012,02,29 和 2012,03,01,则输出为 3。实际答案应为 1。这些还有其他方法可以计算 2 个日期之间的天数吗?

【问题讨论】:

标签: javascript jquery


【解决方案1】:

对我有用 - 请记住 JS 中的月份从 0 开始,所以这里是 2012 年 2 月 29 日到 3 月 1 日

var oneDay = 24 * 60 * 60 * 1000; // hours*minutes*seconds*milliseconds
var firstDate = new Date(2012, 1, 29, 12, 0, 0, 0); // 29th of Feb at noon your timezone
var secondDate = new Date(2012, 2, 1, 12, 0, 0, 0); // 1st of March at noon

var diffDays = Math.round(Math.abs((firstDate.getTime() - secondDate.getTime()) / (oneDay)));
console.log(firstDate, "to", secondDate, "\nDifference: " + diffDays + " day");

【讨论】:

  • 该死,打败我...他的问题可能是从 0 开始的月份。 :)
  • 对不起,这是我的错。非常感谢
【解决方案2】:

我也遇到了同样的问题,成功为此浪费了半天的时间。终于解决了问题。可以看下面的例子:

注意:不要忘记包含 jquery 脚本:

$("#ToDate").change(function() {
  var start = new Date($('#FromDate').val());
  var end = new Date($('#ToDate').val());
  var diff = new Date(end - start);
  var days = 1;
  days = diff / 1000 / 60 / 60 / 24;
  if (days == NaN) {
    $('#TotalDays').val(0);
  } else {
    $('#TotalDays').val(days + 1);
  }
})
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>


<form action="{{route('leave.store')}}" method="post" class="form-horizontal">
  @csrf
  <div class="card-body">
    <h4 class="card-title">Apply Leave</h4>
    <div class="form-group row">
      <label for="fname" class="col-sm-3 text-right control-label col-form-label">Leave type</label>
      <div class="col-sm-9">
        <input type="text" name="leave_type" class="form-control" id="fname" placeholder="Leave type">
      </div>
    </div>
    <div class="form-group row">
      <label for="lname" class="col-sm-3 text-right control-label col-form-label">Date from</label>
      <div class="col-sm-4">
        <input type="date" min="{{date('Y-m-d')}}" name="from" class="form-control" id="FromDate">
      </div>
      <div class="col-sm-4">
        <input type="date" name="to" class="form-control" id="ToDate">
      </div>
    </div>
    <div class="form-group row">
      <label for="fname" class="col-sm-3 text-right control-label col-form-label">Days</label>
      <div class="col-sm-9">
        <input type="text" name="days" class="form-control" id="TotalDays" placeholder="Number of leave days">
      </div>
    </div>
    <div class="form-group row">
      <label for="fname" class="col-sm-3 text-right control-label col-form-label">Reason</label>
      <div class="col-sm-9">
        <textarea type="text" name="reason" class="form-control" placeholder="Reason">
                                        </textarea></div>
    </div>
  </div>
  <div class="border-top">
    <div class="card-body">
      <button type="submit" class="btn btn-dark">Apply</button>
    </div>
  </div>
</form>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多