> 文章列表 > 前端获取时区

前端获取时区

前端获取时区

在前端中,可以使用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