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>