迹忆客 专注技术分享

当前位置:主页 > 学无止境 > WEB前端 > JavaScript >

将 JavaScript 日期初始化为特定时区

作者:迹忆客 最近更新:2023/03/24 浏览次数:

本篇文章介绍如何将 JavaScript 日期初始化为特定时区。

JavaScript 的日期对象在内部使用 UTC,但根据操作计算机的本地时间显示输出。日期对象没有任何时区概念。没有声明字符串对象。它只是对自 1970-01-01 00:00:00 UTC 以来经过的毫秒数的度量。在输出时间时,它会自动考虑计算机的本地时区并应用于内部表示。它不适用于不同的时区。因此,我们需要借助函数和外部库来执行此类操作。


使用 Intl.DateTimeFormatformat() 方法将 JavaScript 日期初始化为特定时区

JavaScript Intl 对象是 JavaScript 国际化 API 的辅助工具。它为我们提供了许多用于转换日期/时间、字符串和数字的函数。Intl 对象使用 DateTimeFormat 来格式化日期时间字符串。它有一个 format 方法,它使用作为格式化程序参数提供的语言环境和选项获取日期并将其转换为不同的时区。此方法会将日期格式化为所需的时区并将其转换为字符串。

format() 函数采用的参数是日期。我们需要以下参数来形成 Intl.DateTimeFormat 对象:

  1. locales:它是一个包含语言和语言环境标签的字符串数组。通常,它是一个可选参数,但对于我们的目的来说是必需的。要更改时区,我们只需指定 BCP 语言代码和所需的时区。
  2. options:它是一个对象,用于指定执行比较的属性。它也是一个可选参数,可用于指定样式和显示单位。一些属性是 secondminutehourday,、monthyear 等。

例如:我们可以传递一个选项对象,如 { hour: 'numeric', hour12: false, minute: 'numeric', timeZoneName: 'short'}

function changeTimezone() {
     
    let date = new Date(Date.UTC(2021, 5, 28, 3, 0, 0));
    console.log('Date in India: ' + date);
    let formatter = new Intl.DateTimeFormat('en-US', { timeZone: "America/Denver" });   
    let usDate = formatter.format(date);
    console.log('Date in USA: ' + usDate);
}

输出:

Date in India: Mon Jun 28 2021 08:30:00 GMT+0530 (India Standard Time)
VM1504:7 Date in USA: 6/27/2021

在上面的函数中,我们首先使用 Date() 构造函数创建一个 Date 对象。然后我们使用 Intl.DateTimeFormat 创建一个格式化程序,指定区域设置,即 BCP 语言标签 和我们要转换到的 timeZone。然后我们使用此格式化程序将日期转换为所需的时区。


使用 toLocaleString() 方法将 JavaScript 日期初始化为特定时区

toLocaleString() 方法最常用于更改时区,因为它更易于使用并且可以直接在 Date 上调用。它的工作方式与 Intl.DateTimeFormat 相同。它还接受语言环境字符串和选项作为参数,并返回一个日期根据它们格式化的字符串。这种方法的优点是不像上面的方法是根据国家的时区转换时间并在字符串中返回。

function changeTimezone() {
      
            let date = new Date(Date.UTC(2021, 5, 28, 3, 0, 0));
            console.log('Date in India: ' + date);
            let usDate = date.toLocaleString("en-US", {timeZone: "America/New_York"});
            console.log('Date in USA: ' + usDate);
        }

输出:

Date in India: Mon Jun 28 2021 08:30:00 GMT+0530 (India Standard Time)
Date in USA: 6/27/2021, 11:00:00 PM

在上面的函数中,我们首先使用 Date() 构造函数创建一个 Date 对象。我们在指定 language tagtimeZone 的日期上调用 toLocaleString 函数,并将日期/时间转换为不同的时区。

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Do you understand JavaScript closures?

发布时间:2025/02/21 浏览次数:108 分类:JavaScript

The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.

Do you know about the hidden traps in variables in JavaScript?

发布时间:2025/02/21 浏览次数:178 分类:JavaScript

Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av

How much do you know about the Prototype Chain?

发布时间:2025/02/21 浏览次数:150 分类:JavaScript

The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start

用 jQuery 检查复选框是否被选中

发布时间:2024/03/24 浏览次数:102 分类:JavaScript

在本教程中学习 jQuery 检查复选框是否被选中的所有很酷的方法。我们展示了使用直接 DOM 操作、提取 JavaScript 属性的 jQuery 方法以及使用 jQuery 选择器的不同方法。你还将找到许多有用的

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便