Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to get local time with Google API?

Tags:

google-api

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?

like image 805
santa Avatar asked Oct 19 '25 10:10

santa


1 Answers

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.

like image 142
MohammedEAmer Avatar answered Oct 22 '25 06:10

MohammedEAmer