
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get Number of Days Between Two Dates in JavaScript
To get the number of days between two dates in JavaScript, we will be understanding four different approaches with stepwise explaination and example codes.
In this article, we are having two different dates and our task is to get the number of days between two dates in JavaScript.
Approaches to Get the Number of Days Between Two Dates
Here is a list of approaches to get the number of days between two dates in JavaScript which we will be discussing in this article with stepwise explaination and complete example codes.
- Get Number of Days Using Date object
- Get Number of Days Using moment.js library
- Get Number of Days Using UTC timestamp
- Get Number of Days Using Current Date
Get Number of Days Using Date object
To get the number of days between two dates, we have used date object which returns day, month, date, year, hours, minutes, seconds and timezone.
- We have initialized two date objects, one with starting date and another with end date.
- Find the time difference between start and end dates in the milliseconds using the getTime() method of the Date object and stored it in diffTime. We have also used Math.abs to get the non-negative value.
- We have divided the diffTime with the total milliseconds in a single day and stored it in diffDays.
- We have used div element with id result to return the output in HTML document itself using getElementById() method.
Example
Here is a complete example code implementing above mentioned steps to get the number of days between two dates in JavaScript using date object.
<html> <head> <title> To get the number of days between two dates </title> </head> <body> <div id="result"></div> <script> let result = document.getElementById('result'); let date1 = new Date('2024-09-23'); let date2 = new Date('2024-10-30'); let diffTime = Math.abs(date2.getTime()-date1.getTime()); let diffDays = diffTime/(1000*60*60*24); result.innerHTML = "Difference in days from " +date1.toDateString() +" to " +date2.toDateString() +" is: " +diffDays; </script> </body> </html>
Get Number of Days Using moment.js library
In this approach to get the number of days between two dates we have used MomentJS library and it's diff method which return the diiference between days in number of days.
- We have used a CDN link to include MomentJS in our HTML document with the help of script tag in head section.
- We have initialized two dates, one with starting date and another with end date using moment() function.
- We have used diff() function of moment.js to get the difference in number of days from date1 to date2.
- We have used div element with id result to return the output in HTML document itself using getElementById() method.
Example
Here is a complete example code implementing above mentioned steps to get the number of days between two dates in JavaScript using moment.js library.
<html> <head> <title> To get the number of days between two dates </title> <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment.min.js"></script> </head> <body> <div id="result"></div> <script> let result = document.getElementById('result'); let date1 = moment('2024-09-23'); let date2 = moment('2024-10-30'); let diffDays = date2.diff(date1, 'days'); result.innerHTML = "Difference in days: " +diffDays; </script> </body> </html>
Get Number of Days Using UTC timestamp
- We have initialized two date objects, one with starting date and another with end date.
- We have used getFullYear, getMonth and getDate to retrieve year, month and date respectively and Date.UTC() method converts the dates to milliseconds.
- We have divided the diffTime of above step with the total milliseconds in a single day and stored it in diffDays.
- We have used toDateString() method to represent the date portion of the dates in result.
- We have used div element with id result to return the output in HTML document itself using getElementById() method.
Example
Here is a complete example code implementing above mentioned steps to get the number of days between two dates in JavaScript using UTC timestamp.
<html> <head> <title> To get the number of days between two dates </title> </head> <body> <div id="result"></div> <script> let result = document.getElementById('result'); let date1 = new Date('2024-09-23'); let date2 = new Date('2024-10-30'); let diffTime = Math.abs(Date.UTC(date2.getFullYear(), date2.getMonth(), date2.getDate()) - Date.UTC(date1.getFullYear(), date1.getMonth(), date1.getDate())); let diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24)); result.innerHTML = "Difference in days from " +date1.toDateString() +" to " +date2.toDateString() +" is: " +diffDays; </script> </body> </html>
Get Number of Days Using Current Date
In this approach, we have used new Date() object to get the current date in milliseconds.
- We have initialized two date objects, one with current date using new date and another with end date.
- All the other steps are similar to approach one to get number of days and we have also used Math.ceil() to avoid getting output in fractions.
Example
Here is a complete example code implementing above mentioned steps to get the number of days between two dates in JavaScript using current date.
<html> <head> <title> To get the number of days between two dates </title> </head> <body> <div id="result"></div> <script> let result = document.getElementById('result'); let curr_date = new Date(); let date2 = new Date('2024-10-30'); let diffTime = Math.abs(date2.getTime()-curr_date.getTime()); let diffDays = Math.ceil(diffTime/(1000*60*60*24)); result.innerHTML = "Difference in days from " +curr_date.toDateString() +" to " +date2.toDateString() +" is: " +diffDays; </script> </body> </html>
Conclusion
In this tutorial, we have understood four different approaches to get the number of days between two dates in JavaScript. The four approaches discussed are: using date object, MomentJS library, UTC timestamp and using current date.