

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
How to get time difference between two timestamps in seconds?
To get the time difference between two timestamps, try to run the following code. Here, we are calculating the total number of hours, minutes and seconds between two timestamps −
Example
<html> <head> <title>JavaScript Dates</title> </head> <body> <script> var date1, date2; date1 = new Date( "Jan 1, 2018 11:10:05" ); document.write(""+date1); date2 = new Date( "Jan 1, 2018 08:15:10" ); document.write("<br>"+date2); var res = Math.abs(date1 - date2) / 1000; // get total days between two dates var days = Math.floor(res / 86400); document.write("<br>Difference (Days): "+days); // get hours var hours = Math.floor(res / 3600) % 24; document.write("<br>Difference (Hours): "+hours); // get minutes var minutes = Math.floor(res / 60) % 60; document.write("<br>Difference (Minutes): "+minutes); // get seconds var seconds = res % 60; document.write("<br>Difference (Seconds): "+seconds); </script> </body> </html>
Output
Mon Jan 01 2018 11:10:05 GMT+0530 (India Standard Time) Mon Jan 01 2018 08:15:10 GMT+0530 (India Standard Time) Difference (Days): 0 Difference (Hours): 2 Difference (Minutes): 54 Difference (Seconds): 55
- Related Questions & Answers
- MySQL difference between two timestamps in Seconds?
- Get the difference between two timestamps in seconds in MySQL?
- Difference between two timestamps in seconds in MySQL?
- How to get the seconds and minutes between two Instant timestamps in Java
- Java Program to get the difference between two time zones by seconds
- Python program to find difference between two timestamps
- C# Program to get the difference between two dates in seconds
- How to get the duration between two Instant timestamps in Java
- Find the difference between two timestamps in days with MySQL
- What is the difference between UNIX TIMESTAMPS and MySQL TIMESTAMPS?
- How to get the number of seconds between two Dates in JavaScript?
- How to get current date/time in seconds in JavaScript?
- Get current date/time in seconds - JavaScript?
- How to get current date/time in milli seconds in Java?
- Difference between two given time periods in C++
Advertisements