JavaScript 的 Date 对象中的 toLocaleTimeString 方法可根据本地时间把 Date 对象的时间部分转换为 字符串,并返回结果。
toLocaleTimeString() 方法可根据本地时间把 Date 对象的时间部分转换为字符串,并返回结果。
dateObject.toLocaleTimeString();
类型 | 描述 |
---|---|
String | dateObject 的时间部分的字符串表示,以本地时间区表示,并根据本地规则格式化。 |
该函数支持所有的主流浏览器。
该函数只支持 JavaScript 1.0 即以上版本。
使用 toLocaleTimeString 将时间转本地时间字符串
<!DOCTYPE html>
<html>
<head>
<title>JavaScript toLocaleTimeString</title>
<script type="text/javascript">
console.log("嗨客网(www.haicoder.net)");
var date = new Date("2022/07/11 17:50:09:110");
var sLocalDate = date.toLocaleTimeString();
console.log("date =", date, "sLocalDate =", sLocalDate);
var date2 = new Date("2022/11/13 20:50:19:109");
var sLocalDate2 = date2.toLocaleTimeString();
console.log("date2 =", date2, "sLocalDate2 =", sLocalDate2);
</script>
</head>
</html>
程序运行后,控制台输出如下:
我们分别创建了两个 Date 对象,即 date 和 date2,接着,我们使用 date 对象的 toLocaleTimeString 方法分别将两个 Date 对象转成了本地时间字符串形式。
JavaScript 的 Date 对象中的 toLocaleTimeString 方法可根据本地时间把 Date 对象的时间部分转换为字符串,并返回结果。JavaScript toLocaleTimeString 语法:
dateObject.toLocaleTimeString();