4

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?

new name
  • 15,861
  • 19
  • 68
  • 114
santa
  • 12,234
  • 49
  • 155
  • 255

1 Answers1

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