如何在 JavaScript 中获取当前日期/时间(以秒为单位)?
若要转换时间为秒,首先获取当前时间。然后将小时乘以 3600,将分钟乘以 60;其余步骤见下文。
(hours*3600) + (min*60) + sec
示例
可以尝试运行以下代码以获取当前时间(以秒为单位)
<html>
<head>
<title>JavaScript Get Seconds</title>
</head>
<body>
<script>
var dt = new Date();
var sec = dt.getSeconds();
document.write("Seconds: " + sec);
var min = dt.getMinutes();
document.write("<br>Minutes: " + min);
var hrs = dt.getHours();
document.write("<br>Hours: " + hrs);
var total_seconds = (hrs*3600) + (min*60) + sec;
document.write("<br>Total seconds: " + total_seconds) ;
</script>
</body>
</html>输出
Seconds: 35 Minutes: 37 Hours: 9 Total seconds: 34655
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
JavaScript
PHP