JavaScript 中的时区列表主要通过 Intl.DateTimeFormat().resolvedOptions().timeZone
来获取。这个方法返回的是一个包含所有可用时区的数组。以下是一些基础概念和相关信息:
Asia/Shanghai
,始终表示同一个时间偏移。Europe/London
,会根据夏令时的变化调整时间。以下是一个简单的示例,展示如何在 JavaScript 中获取和使用时区列表:
// 获取所有可用的时区
const timeZoneList = Intl.DateTimeFormat().resolvedOptions().timeZone;
console.log(timeZoneList);
// 使用特定时区格式化日期
const date = new Date();
const options = { timeZone: 'Asia/Shanghai', year: 'numeric', month: 'long', day: 'numeric', hour: '2-digit', minute: '2-digit', second: '2-digit' };
const formatter = new Intl.DateTimeFormat('zh-CN', options);
console.log(formatter.format(date)); // 输出当前上海时间
moment-timezone
)来获取更全面的时区数据。// 使用 moment-timezone 库获取时区列表
const moment = require('moment-timezone');
console.log(moment.tz.names());
moment-timezone
)。// 使用 moment-timezone 处理夏令时
const summerTimeDate = moment.tz('2023-07-01 12:00:00', 'Europe/London').format();
console.log(summerTimeDate); // 输出伦敦夏季时间
通过以上方法,可以有效管理和处理 JavaScript 中的时区相关问题。
没有搜到相关的文章