要获取两个日期之间的秒数,请使用Maths.abs()方法。
您可以尝试运行以下代码以获取两个日期之间的秒数-
<html> <head> <title>JavaScript Get Seconds</title> </head> <body> <script> var date1, date2; date1 = new Date(); document.write(""+date1); date2 = new Date( "Dec 10, 2015 20:15:10" ); document.write("<br>"+date2); //获取两个日期之间的总秒数 var seconds = Math.abs(date1 - date2) / 1000; document.write("<br>Difference between 2 dates: "+seconds); </script> </body> </html>
输出结果
Mon May 28 2018 09:35:25 GMT+0530 (India Standard Time) Thu Dec 10 2015 20:15:10 GMT+0530 (India Standard Time) Difference between 2 dates: 77721615.573