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>

4
  • 1
    Please have a look in moment.js, which has the ability to handle durations... Commented Aug 29, 2017 at 7:51
  • stackoverflow.com/questions/11170054/… Commented Aug 29, 2017 at 7:53
  • Thank you.I am spending more time for moments.js now ,for referring. @Myonara Commented Aug 29, 2017 at 10:52
  • @TranAudi I went through , but it didn't help . Used the moments .Thanks Commented Aug 29, 2017 at 10:53

3 Answers 3

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>

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks it worked. New to moments.Quite fascinating.Cheers!
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);

1 Comment

what about if user change the month ?
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 :)

2 Comments

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
you are welcome @Tithira. yep, it's a good solution if you don't want dependencies.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.