- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to calculate the difference between two dates in JavaScript?
To get dates in JavaScript, use the getTime() method. Forgetting the difference between two dates, calculate the difference between date and time.
Example
You can try to run the following code to learn how to calculate a difference between two dates −
<!DOCTYPE html> <html> <body> <script> var dateFirst = new Date("11/25/2017"); var dateSecond = new Date("11/28/2017"); // time difference var timeDiff = Math.abs(dateSecond.getTime() - dateFirst.getTime()); // days difference var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24)); // difference alert(diffDays); </script> </body> </html>
Advertisements