PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

如何使用 JavaScript 将 UTC 日期时间转换为本地日期时间?

WBOY
WBOY 转载
2023-09-12 18:05:04 351浏览

如何使用 JavaScript 将 UTC 日期时间转换为本地日期时间?

时区处理是每个 Web 应用程序的重要组成部分。后端记录的时间通常采用UTC格式。然而,当它显示给用户时,必须将其转换为用户的本地时间。这可以通过 JavaScript 实现。我们将在本博客中了解如何使用 JavaScript 将 UTC 日期时间转换为本地日期时间。

JavaScript 日期

JavaScript 包含一个“Date”类,允许我们处理日期和时间。 Date 类包含各种处理日期和时间的方法,包括 -

Date() - 以毫秒为单位返回当前日期和时间 getTime() 以毫秒为单位返回当前时间

getUTCFullYear() - 返回 UTC 时区的日期年份。

getUTCMonth() - 返回 UTC 时区中的日期月份。

getUTCDate() - 返回 UTC 时区中日期的月份和日期。

getUTCHours() - 返回 UTC 时区的日期小时。

getUTCMinutes() - 返回 UTC 时区的日期分钟数。

getUTCSeconds() - 返回 UTC 时区中日期的秒数。

将 UTC 转换为本地时间

我们必须使用 getTimezoneOffset() 方法将 UTC 日期时间转换为本地日期时间。此方法返回 UTC 和本地时间之间的时差(以分钟为单位)。然后可以使用此分钟差异将 UTC 日期时间转换为本地日期时间。

示例

例如,以下代码将 UTC 日期时间转换为本地日期时间 -

<!DOCTYPE html>
<html>
<body>
   <div id="result"></div>
   <script>
      var utc = new Date();
      var offset = utc.getTimezoneOffset();
      var local = new Date(utc.getTime() + offset * 60000);
      document.getElementById("result").innerHTML = local;
   </script> 
</body>
</html>

我们可以在这里看到一个名为“utc”的 New Date 对象,它保存当前的 UTC 日期和时间。然后我们使用 getTimezoneOffset() 函数来计算 UTC 和本地时间之间的时差(以分钟为单位)。最后,我们通过将此量与 UTC 时间(以毫秒为单位)相加来计算本地时间。

Similarly, we can convert a specified UTC date time into a local date time. To accomplish the same thing, simply supply the UTC date and time as parameters to the Date() function Object() { [native code] }. Now, let's see the code to convert a UTC date time of "2018-11-12 12:00:00" to a local date time −

示例

<!DOCTYPE html>
<html>
<head>
   <title>Date Example</title>
</head>
<body>
   <div id="result"></div>
   <script>
      var utc = new Date("2018-11-12 12:00:00");
      var offset = utc.getTimezoneOffset();
      var local = new Date(utc.getTime() + offset * 60000);
      document.getElementById("result").innerHTML = "UTC : " + utc + "<br>" + "Local : " + local;
   </script>
</body>
</html>

我们已将 UTC 日期和时间作为字符串传递给 Date() 构造函数。然后我们使用与之前相同的方法将 UTC 日期时间转换为本地日期时间。

将当地时间转换为 UTC

Now, how can we get from local time to UTC? To convert local date time to UTC date time, we can use the getTimezoneOffset() method once more. Since this function returns the time difference in minutes between UTC and local time. This difference number can be used to convert the local date time to the UTC date time.

示例

例如,以下代码将本地日期时间转换为 UTC 日期时间 -

<!DOCTYPE html>
<html>
<body>
   <div id="result"></div>
   <script>
      var local = new Date();
      var offset = local.getTimezoneOffset();
      var utc = new Date(local.getTime() - offset * 60000);
      document.getElementById("result").innerHTML = utc;
   </script>
</body>
</html>

在上面的代码中,我们首先创建了一个名为“local”的新 Date 对象,其中包含当前的本地日期和时间。然后,我们使用 getTimezoneOffset() 方法获取 UTC 与本地时间之间的时差(以分钟为单位)。从本地时间(以毫秒为单位)减去该值后,我们得到了 UTC 时间。

We can also convert a specific local date time into a UTC date time by passing the local date and time as arguments to the Date() constructor. For example, the following code will convert a local date time of "2018-11-12 12:00:00" into the UTC date time −

示例

<!DOCTYPE html>
<html>
<body>
   <div id="result"></div>
   <script>
      var local = new Date("2018-11-12 12:00:00");
      var offset = local.getTimezoneOffset();
      var utc = new Date(local.getTime() - offset * 60000);
      document.getElementById("result").innerHTML = utc;
   </script> 
</body>
</html>

我们将本地日期和时间作为字符串传递给 Date() 构造函数。然后我们使用与之前相同的方法将本地日期时间转换为 UTC 日期时间。

结论

在本教程中,我们学习了如何使用 JavaScript 将 UTC 日期时间转换为本地日期时间。我们还了解到,JavaScript Date 类提供了多种处理日期和时间的方法,例如 getTimezoneOffset(),可用于将 UTC 日期时间转换为本地日期时间。我们还学习了如何使用本博客中的相同方法将本地日期时间转换为 UTC 日期时间。需要注意的是,时区处理是任何 Web 应用程序的一个重要方面,正确转换时间以便正确显示给用户也很重要。

以上就是如何使用 JavaScript 将 UTC 日期时间转换为本地日期时间?的详细内容,更多请关注php中文网其它相关文章!

声明:本文转载于:tutorialspoint,如有侵犯,请联系admin@php.cn删除