tilak
tilak

Reputation: 4691

How to get the number of days between two dates?

I need to calculate the number of days between 2 date. I have check the codes given this link How to calculate the number of days between two dates using JavaScript?.

In this example if give input 2012,02,29 and 2012,03,01 it gives output as 3. Actual answer should be 1. These there any other methods to calculate the number of days between 2 dates ?

Upvotes: 3

Views: 12760

Answers (2)

Ganesh Khadka
Ganesh Khadka

Reputation: 87

I was also facing the same problem and successfully wasted my half of the day for this.And finally sort out the problem.You can see the below example:

Note:Don't forget to include the jquery script:

$("#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>

Upvotes: 0

mplungjan
mplungjan

Reputation: 177692

Works for me - remember months in JS start at 0 so here are February 29th to March 1st 2012

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");

Upvotes: 12

Related Questions