Get the number of days between two dates using jQuery
Today, we’ll explain to you how to get the number of days between two dates using jQuery.
Sometimes, we need to calculate the number of days between two dates. Using getTime() function, we can easily do this task.
Here, we will create the HTML form where we will take two input fields with datepicker and one button to calculate the number of days.
Write the following code and run to check output.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | <!DOCTYPE html> <html> <head> <title>Get the number of days between two dates using jQuery - Clue Mediator</title> <!-- CSS --> <link href='https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css' rel='stylesheet' type='text/css'> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css"> <!-- JS --> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script> </head> <body> <div class="container"> <div class="col-md-8 mt-4 "> <h5>Get the number of days between two dates using jQuery - <a href="https://www.cluemediator.com" target="_blank" rel="noopener noreferrer">Clue Mediator</a></h5> <form class="form-inline"> <div class="form-group mb-2"> <input type="text" class="form-control datepicker mr-2" id="fromdate" /> <input type="text" class="form-control datepicker" id="todate" /> </div> <div class="form-group mb-2"> <input type="button" class="btn btn-primary m-2" value="Calculate" id="calculate"> </div> </form> <div id="result"></div> </div> </div> <script> $('.datepicker').datepicker({ dateFormat: 'yy-mm-dd', changeMonth: true, changeYear: true, }); $(document).on('click', '#calculate', function () { var fromdate = $("#fromdate").val(); var todate = $("#todate").val(); if ((fromdate == "") || (todate == "")) { $("#result").html("Please enter two dates"); return false } var dt1 = new Date(fromdate); var dt2 = new Date(todate); var time_difference = dt2.getTime() - dt1.getTime(); var result = time_difference / (1000 * 60 * 60 * 24); var output = "Total number of days between dates - " + result; $("#result").html(output); }); </script> </body> </html> |
How it works
- Get the value of two dates.
- Initialize them by creating the date objects using
new Date()
. - Calculate the time difference of two dates using
dt2.getTime() - dt1.getTime()
. - Calculate the no. of days by dividing total milliseconds (1000 * 60 * 60 * 24).
- Print the result in div with id result.
Output
That’s it for today.
Thank you for reading. Happy Coding..!! 🙂