Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
361 views
in Technique[技术] by (71.8m points)

datetime - How to get the real date and time of the country using Javascript even if the device time is wrong?

Is there a way to get the actual date and time of the country in Javascript even if the device time is inaccurate?

Say:

  1. I am in Austin Texas, US (GMT-6) and it is Jan 27, 2021, 9:20pm
  2. I change the device date and time Feb 1, 2021, 6:03pm
  3. Using Javascript, how do I get the actual date and time in Mumbai India?
question from:https://stackoverflow.com/questions/65930585/how-to-get-the-real-date-and-time-of-the-country-using-javascript-even-if-the-de

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There is no way to retrieve actual date/time from your device alone unless its system time is accurate.

Using Javascript (and a bit of help from Fetch API), you can always get current time from the third-party services like this.

World Time API

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...