【问题标题】:How to get the real date and time of the country using Javascript even if the device time is wrong?即使设备时间错误,如何使用 Javascript 获取国家/地区的实际日期和时间?
【发布时间】:2021-01-28 03:23:44
【问题描述】:

即使设备时间不准确,有没有办法在 Javascript 中获取国家/地区的实际日期和时间?

说:

  1. 我在美国德克萨斯州奥斯汀 (GMT-6),现在是 2021 年 1 月 27 日晚上 9:20
  2. 我更改设备日期和时间 2021 年 2 月 1 日下午 6:03
  3. 使用 Javascript,如何获取印度孟买的实际日期和时间?

【问题讨论】:

  • JavaScript 中的 new Date() 将始终从用户设备读取时间。要处理这个问题,您需要从服务器获取日期时间。
  • 在客户端,你不能。除非您向为您提供准确时间信息的外部服务提出请求。

标签: javascript datetime time date-format


【解决方案1】:

除非系统时间准确,否则无法单独从您的设备中检索实际日期/时间。

使用 Javascript(以及来自 Fetch API 的一些帮助),您始终可以像这样从第三方服务获取当前时间。

World Time API

fetch('http://worldtimeapi.org/api/timezone/Europe/Belgrade')
  .then(response => response.json())
  .then(data => console.log(data));

【讨论】:

  • 太棒了。这真的很有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-23
  • 2012-08-25
  • 2021-12-29
  • 1970-01-01
  • 1970-01-01
  • 2015-07-20
相关资源
最近更新 更多