1

I am a beginner in JS. I have user input values of the date. I want to get the difference in days. Tried several methods, none of those worked. Used the input type as numbers but it doesn't work for an example like 03092017 - 29082017, it outputs the number difference not the date.

function resetForm() {
  location.reload();
}

function dates() {
  var n_Date = document.getElementById('ddate').value;
  var e_Date = document.getElementById('cdate').value;
  var nDate = new Date(n_Date);
  var eDate = new Date(e_Date);
  var nMonth = n_Date.getMonth();
  var eMonth = eDate.getMonth();
  var nYear = nDate.getFullYear();
  var eYear = eDate.getFullYear();
  var Years = Math.abs(nYear - eYear);
  var Months = Math.abs(nMonth - eMonth);
  var Dates = Math.abs(nDate - eDate);
  var fDate = Dates + Months + Years;

  if (fDate <= 7) {
    alert("You have: " + fDate + " days to return the book");
  } else if (eDate > 7) {
    var g = (fDate * 50);
    alert("Overdue by: " + fDate + " days fee is: " + g);
  }

}
<form id="myform">
  <table>
    <tr>
      <td>Book Returning Due Date:</td>
      <td><input type="date" id="ddate" placeholder="dd/mm/yyyy"></td>
    </tr>
    <tr>
      <td>Current Date:</td>
      <td><input type="date" id="cdate" placeholder="dd/mm/yyyy"></td>
    </tr>
    <tr>
      <td><input type="button" onclick="dates();" value="Check for Due"> </td>
      <td><input type="button" id="reset" onclick="resetForm()" value="Reset"></td>
    </tr>

  </table>
</form>
Alessio Cantarella
  • 5,077
  • 3
  • 27
  • 34
Tithira
  • 634
  • 9
  • 22

3 Answers3

1

check below sample solution to get diff between two dates in days

var nDate  = new Date(n_Date);
var eDate  = new Date(e_Date);
var diffInDays = nDate.getDate()-eDate.getDate()

here is solution with sample data:

 var nDate  = new Date("2017-08-29");
    var eDate  = new Date("2017-08-25");
    var diffInDays = nDate.getDate()-eDate.getDate()
console.log(diffInDays);
Nemani
  • 778
  • 5
  • 12
1

Try to use moment.js for this.

var startDate = moment("13.04.2016", "DD.MM.YYYY");
var endDate = moment("28.05.2016", "DD.MM.YYYY");
var result = 'Diff: ' + endDate.diff(startDate, 'days');
  
  console.log(result);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://momentjs.com/downloads/moment.js"></script>
chirag satapara
  • 1,947
  • 1
  • 15
  • 26
0

you can do something like this:

var startDate = new Date(2017,08,29);
var endDate = new Date(2017,09,15);
var oneDayMilliseconds = 3600*24*1000;

var daysLeft = Math.round(Math.abs((startDate.getTime() - endDate.getTime())/(oneDayMilliseconds)));

console.log("Days left: " + daysLeft);

Hope it helps :)

MrGoodKat
  • 41
  • 6
  • Thank you so much for the great help.I was able to do it in this way too. `(parseInt((pickdt- dropdt) / (24 * 3600 * 1000)))` and got the value to do the math as in [link](https://stackoverflow.com/questions/26216565/calculate-number-of-days-between-two-input-type-date-in-html) – Tithira Aug 29 '17 at 10:57
  • you are welcome @Tithira. yep, it's a good solution if you don't want dependencies. – MrGoodKat Aug 29 '17 at 12:06