Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How to get time difference between two timestamps in seconds?
To get the time difference between two timestamps in seconds, you can subtract one Date object from another and divide by 1000. JavaScript Date objects return milliseconds when subtracted, so dividing by 1000 converts to seconds.
Basic Time Difference in Seconds
The simplest approach is to subtract two Date objects and convert milliseconds to seconds:
<html>
<head>
<title>Time Difference in Seconds</title>
</head>
<body>
<script>
var date1 = new Date("Jan 1, 2018 11:10:05");
var date2 = new Date("Jan 1, 2018 08:15:10");
document.write("Start Time: " + date1 + "<br>");
document.write("End Time: " + date2 + "<br>");
// Calculate difference in seconds
var diffInSeconds = Math.abs(date1 - date2) / 1000;
document.write("<br>Total Difference: " + diffInSeconds + " seconds");
</script>
</body>
</html>
Start Time: Mon Jan 01 2018 11:10:05 GMT+0530 (India Standard Time) End Time: Mon Jan 01 2018 08:15:10 GMT+0530 (India Standard Time) Total Difference: 10495 seconds
Breaking Down Time Components
You can also extract days, hours, minutes, and seconds from the total difference:
<html>
<head>
<title>Time Difference Breakdown</title>
</head>
<body>
<script>
var date1 = new Date("Jan 1, 2018 11:10:05");
var date2 = new Date("Jan 1, 2018 08:15:10");
var totalSeconds = Math.abs(date1 - date2) / 1000;
// Calculate individual components
var days = Math.floor(totalSeconds / 86400);
var hours = Math.floor(totalSeconds / 3600) % 24;
var minutes = Math.floor(totalSeconds / 60) % 60;
var seconds = totalSeconds % 60;
document.write("Total Seconds: " + totalSeconds + "<br>");
document.write("Days: " + days + "<br>");
document.write("Hours: " + hours + "<br>");
document.write("Minutes: " + minutes + "<br>");
document.write("Seconds: " + seconds);
</script>
</body>
</html>
Total Seconds: 10495 Days: 0 Hours: 2 Minutes: 54 Seconds: 55
Using Timestamps
You can also work directly with timestamps using getTime() method:
<html>
<head>
<title>Using Timestamps</title>
</head>
<body>
<script>
var date1 = new Date("Jan 1, 2018 11:10:05");
var date2 = new Date("Jan 1, 2018 08:15:10");
var timestamp1 = date1.getTime();
var timestamp2 = date2.getTime();
document.write("Timestamp 1: " + timestamp1 + "<br>");
document.write("Timestamp 2: " + timestamp2 + "<br>");
var diffInSeconds = Math.abs(timestamp1 - timestamp2) / 1000;
document.write("<br>Difference: " + diffInSeconds + " seconds");
</script>
</body>
</html>
Timestamp 1: 1514778005000 Timestamp 2: 1514767510000 Difference: 10495 seconds
Key Points
- Subtracting Date objects returns difference in milliseconds
- Divide by 1000 to convert milliseconds to seconds
- Use
Math.abs()to ensure positive result regardless of date order - Use
getTime()for explicit timestamp manipulation
Conclusion
Getting time difference in seconds is straightforward: subtract two Date objects and divide by 1000. Use Math.abs() to handle date ordering automatically.
Advertisements
