Tính hiệu mili-giây, chuyển đổi ra ngày, giờ, phút, giây.
Ví dụ
<html>
<head>
<title>Chênh lệch thời gian giữa hai timestamp JavaScript</title>
</head>
<body>
<script>
var date1 = new Date("Jan 1, 2018 11:10:05");
document.write(date1 + "<br>");
var date2 = new Date("Jan 1, 2018 08:15:10");
document.write(date2 + "<br>");
var res = Math.abs(date1 - date2) / 1000; // tổng giây
var days = Math.floor(res / 86400);
var hours = Math.floor(res / 3600) % 24;
var minutes = Math.floor(res / 60) % 60;
var seconds = res % 60;
document.write("<br>Ngày: " + days);
document.write("<br>Giờ: " + hours);
document.write("<br>Phút: " + minutes);
document.write("<br>Giây: " + seconds);
</script>
</body>
</html>
Kết quả
Thứ 2, 1 Thg 1 2018 11:10:05 GMT+0530 Thứ 2, 1 Thg 1 2018 08:15:10 GMT+0530 Ngày: 0 Giờ: 2 Phút: 54 Giây: 55