如何使用 JavaScript 获取当前时区和 UTC 时间
在前端中,可以使用 JavaScript 的 Date 对象来获取当前时区的时间。
代码示例:
const now = new Date();
const localTime = now.toLocaleTimeString();
const localDate = now.toLocaleDateString();
console.log('Local time is ' + localTime + ' on ' + localDate);
这段代码会输出当前时区的时间和日期,例如:
Local time is 11:30:00 AM on 9/28/2021
如果需要获取 UTC 时间,可以使用以下代码:
const now = new Date();
const utcTime = now.toUTCString();
console.log('UTC time is ' + utcTime);
这段代码会输出当前 UTC 时间,例如:
UTC time is Tue, 28 Sep 2021 03:30:00 GMT
原文地址: https://www.cveoy.top/t/topic/jqou 著作权归作者所有。请勿转载和采集!