You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将GMT日期字符串转换为PST格式并输出mm/dd/yyyy - hh:mi格式

实现方案

你可以直接使用JavaScript原生的Intl.DateTimeFormatAPI完成时区转换和格式化,无需手动计算时差,还能自动适配太平洋时区的夏令时切换,实现代码如下:

// 你的原始日期数组
const getDaysArray = [
    "2021-10-28T08:00:00",
    "2021-10-27T08:00:00",
    "2021-10-31T03:00:00"
];
// PST日期格式化函数
function formatToPST(isoDateStr) {
    const date = new Date(isoDateStr);
    // 配置格式化参数,指定时区为美国洛杉矶(对应太平洋时区)
    const formatOptions = {
        timeZone: 'America/Los_Angeles',
        year: 'numeric',
        month: '2-digit',
        day: '2-digit',
        hour: '2-digit',
        minute: '2-digit',
        hour12: true
    };
    // 提取各部分日期值
    const dateParts = new Intl.DateTimeFormat('en-US', formatOptions).formatToParts(date);
    const month = dateParts.find(item => item.type === 'month').value;
    const day = dateParts.find(item => item.type === 'day').value;
    const year = dateParts.find(item => item.type === 'year').value;
    const hour = dateParts.find(item => item.type === 'hour').value;
    const minute = dateParts.find(item => item.type === 'minute').value;
    const dayPeriod = dateParts.find(item => item.type === 'dayPeriod').value;
    // 拼接成要求的输出格式
    return `${month}/${day}/${year} - ${hour}:${minute} ${dayPeriod} Pacific Time`;
}
// 遍历输出结果
for(let i = 0; i < getDaysArray.length; i++) {
    document.write(formatToPST(getDaysArray[i]) + '<br>');
}

运行输出示例

以上代码运行后会输出以下结果:

  • 10/28/2021 - 01:00 AM Pacific Time
  • 10/27/2021 - 01:00 AM Pacific Time
  • 10/30/2021 - 08:00 PM Pacific Time

如果不需要后缀的Pacific Time,直接删除返回值里的对应字符串即可。


内容的提问来源于stack exchange,提问作者Synchro

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 05:36:03