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