Came across a requirement to find the difference between two dates using JavaScript:
After quite some research figured out two different ways to find the date difference:
- Using moment.js Plugin:
- The moment.js plugin can be found here: http://momentjs.com/
- This is a very useful plugin for Date manipulation and provides various functions for manipulating Dates including finding difference between two days
- Using standard JavaScript:
- However, this can also be done using standard JavaScript if you do not want to add a plugin, using the following:
<script type="text/javascript">
var a = new Date("06/02/2018");
var b = new Date("06/03/2018");
var difference = (b - a) / (1000 * 60 * 60 * 24);
</script>
The formula used above is explained as below:
1000 milliseconds = 1 second
60 seconds = 1 minute
60 minutes = 1 hour
24 hours = 1 day