迹忆客 专注技术分享

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

使用 JavaScript 获取 UTC 时间

作者:迹忆客 最近更新:2022/10/06 浏览次数:

使用 toUTCString() 方法获取 UTC 时间,例如 new Date().toUTCString()。 该方法使用 UTC 时区将日期转换为字符串,该时区与 GMT 共享相同的当前时间。

const date = new Date();

// ✅ 使用 UTC (= GMT) 时区获取表示给定日期的字符串。
const gmtDateTime = date.toUTCString();
console.log(gmtDateTime); // 👉️ "Sat, 15 Jan 2022 07:01:25 GMT"

// 👇️ "07:01:25"
console.log(
  [
    padTo2Digits(date.getUTCHours()),
    padTo2Digits(date.getUTCMinutes()),
    padTo2Digits(date.getUTCSeconds()),
  ].join(':'),
);

function padTo2Digits(num) {
  return num.toString().padStart(2, '0');
}

GMTUTC 共享相同的当前时间。

它们之间的区别在于 GMT 是一个时区,而 UTC 是一个时间标准,是全球时区的基础。

UTCGMT 不会因夏令时 (DST) 而改变,并且始终共享相同的当前时间。

我们使用 toUTCString 方法获取一个字符串,该字符串表示使用 GMT 时区的给定日期。

如果我们只需要时间,则可以使用第二个示例,它将 GMT 时间格式化为 hh:mm:ss

我们可以使用任何可用的 getUTC* 方法,因为它们根据通用时间 (= GMT) 返回日期和时间组件。

function padTo2Digits(num) {
  return num.toString().padStart(2, '0');
}

function getGMTTime(date = new Date()) {
  return [
    padTo2Digits(date.getUTCHours()),
    padTo2Digits(date.getUTCMinutes()),
    padTo2Digits(date.getUTCSeconds()),
  ].join(':');
}

console.log(getGMTTime()); // 👉️️ "07:01:23"

我们创建了一个可重用的函数,它返回 GMT 时间,格式为 hh:mm:ss。

getUTCHours 方法根据通用时间 (= GMT) 返回指定日期的小时 (0 - 23)。

getUTCMinutes 方法根据通用时间 (= GMT) 返回日期的分钟 (0 - 59)。

getUTCSeconds 方法根据通用时间 (= GMT) 返回日期的秒数 (0 - 59)。

如果我们需要使用任何其他 getUTC* 方法,例如 getUTCMonth,访问 MDN 文档

在函数中,我们确保将小时、秒和分钟显示为 2 位数字,即使它们小于 10。

默认情况下,如果任何值小于 10,则方法返回单个数字,这不是我们想要的。

如有必要,我们会填充结果并使用冒号分隔符将它们连接起来。

我们可以根据用例进行调整,例如在格式化字符串中包含年、月、日值。

这些方法中的每一个都有一个非 UTC 等效项,例如 getUTCFullYeargetFullYear

getUTC* 方法根据通用时间 (= GMT) 返回日期或时间部分,而 get* 方法根据本地时间(访问者计算机所在的时区)返回它们。

get* 方法会根据用户从何处访问我们的站点返回不同的结果。

例如,如果我们将当地时间午夜 (00:00) 存储在数据库中,我们将不知道这是东京(日本)、巴黎(法国)、纽约(美国)等的午夜。这些都是相隔数小时的不同时刻。

为了保持一致性,当我们必须向用户呈现日期和时间时,我们应该主要使用本地时间,但将实际值存储在 UTC (=GMT) 中。

转载请发邮件至 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

JavaScript POST

发布时间:2024/03/23 浏览次数:96 分类:JavaScript

本教程讲解如何在不使用 JavaScript 表单的情况下发送 POST 数据。

扫一扫阅读全部技术教程

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

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便