I'm working on an app that it using Google API for maps, but the problem I am having is obtaining correct local time. I can not use JS, because a user can travel to a different location and probably won't change the time settings. Also the server seems to be somewhat off and I have no control over its UNIX time. However, anytime you google for local time, you get based on your location. Since my app is using location as well, shouldn't I be able to access that time as well?
Asked
Active
Viewed 1.4k times
1 Answers
5
This can be done using Google Timezone API:
1- First use Google Timezone API to obtain the user's time zone info, the api accepts the target time (timestamp) in UTC (as UNIX time in secs) and location data
2- The response contains three important fields:
- dstOffset: This is offset for day-light saving (secs)
- rawOffset: This is raw offset for given location without day-light saving (secs)
- timeZoneId: This is IANA time zone id
Using the first two you can get your date in JS as:
var localDate = new Date((timestamp + dstOffset + rawOffset) * 1000);
or by third one using timezone-js or moment-timezone.
Have a look at this answer for using timezone.

Community
- 1
- 1

MohammedEAmer
- 119
- 2
-
1Why multiply by 1,000? – J. Martin Apr 14 '17 at 22:17
-
Sorry for late reply. This is because the Date constructor accepts UNIX epoch in milliseconds and the returned data is in seconds. 1 sec = 1000 msec – MohammedEAmer May 25 '17 at 07:36