公告:魔扣目录网为广大站长提供免费收录网站服务,提交前请做好本站友链:【 网站目录:https://www.morko.net 】, 免友链快审服务(50元/站),

点击这里在线咨询客服
新站提交
  • 网站:51998
  • 待审:31
  • 小程序:12
  • 文章:1030137
  • 会员:747

时区处理是每个 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 日期时间转换为本地日期时间?的详细内容,更多请关注www.92cms.cn其它相关文章!

分享到:
标签:UTC 如何使用 日期 时间 转换为
用户无头像

网友整理

注册时间:

网站:5 个   小程序:0 个  文章:12 篇

  • 51998

    网站

  • 12

    小程序

  • 1030137

    文章

  • 747

    会员

赶快注册账号,推广您的网站吧!
最新入驻小程序

数独大挑战2018-06-03

数独一种数学游戏,玩家需要根据9

答题星2018-06-03

您可以通过答题星轻松地创建试卷

全阶人生考试2018-06-03

各种考试题,题库,初中,高中,大学四六

运动步数有氧达人2018-06-03

记录运动步数,积累氧气值。还可偷

每日养生app2018-06-03

每日养生,天天健康

体育训练成绩评定2018-06-03

通用课目体育训练成绩评定