1

I am trying to get the time from GPS with Javascript, I've found the Geolocation API (https://developer.mozilla.org/en-US/docs/Web/API/Geolocation_API.

It give me a GeolocationPosition (https://developer.mozilla.org/en-US/docs/Web/API/GeolocationPosition). But, the GeolocationPosition.timestamp contain time info from the device. If I change the hours on it, the return change too.

I would like get the time from the GPS when I ask it. The goal is to do a kind of Chronometer on 2 devices (one for start, an other for end) but, then I do the difference. But to do that, I need to have exactly the same time on each.

mpiot
  • 1,482
  • 2
  • 15
  • 36
  • You can also use ```new Date().getTime()```, but with javascipt you will always depend on the date and time of the device – Daniel Jan 20 '20 at 07:50
  • Okay thanks a lot, do you know if with ReactNative it's possible to depend on the GPS date and time ? – mpiot Jan 20 '20 at 08:19
  • I've never used ReactNative so I can't give you an accurate answer – Daniel Jan 20 '20 at 10:05

1 Answers1

0

I can see 2 ways of achieving this:

  1. Use a third-party API to get the current time, irrespective of the user's machine's time. However, the options for such APIs appear to be limited and not well-backed. If the stakes are low, you can try one of these:

  2. Get the user's time zone based on their GPS (see here for many options), then get the time from your server and convert as necessary. This will require a little back-end coding to provide the response and also making sure your server's date/time are always correct. But it's probably better than making your app depend on a third-party API that may be taken down at any time.

If you need code specific to React, please provide what you have so far.

sfarbota
  • 2,619
  • 1
  • 22
  • 30