定义和用法
toTimeString() 方法可把 Date 对象的时间部分转换为字符串,并返回结果。
语法
Date.toTimeString()
返回值
| Type | 描述 |
|---|---|
| String | 时间作为字符串输出 |
浏览器支持
所有主要浏览器都支持 toTimeString() 方法。
实例
把 Date 对象的时间部分转换为字符串:
<p id="demo">单击按钮来显示时间为字符串。</p>
<button onclick="myFunction()">点我</button>
<script>
function myFunction(){
var d = new Date();
var x = document.getElementById("demo");
x.innerHTML=d.toTimeString();
}
</script>