0

offset = new Date().getTimezoneOffset();

This gives you timezone offset, based on computer clock or browser's timezone. Is there any way to get timezone offset based on ip location?

Chris
  • 4,762
  • 3
  • 44
  • 79
  • 1
    Not without a third party service providing it – charlietfl May 29 '18 at 20:08
  • This has some good suggestions: https://stackoverflow.com/questions/13/determine-a-users-timezone. One of them being asking the users their timezone. – Adam May 29 '18 at 20:11

2 Answers2

0

There's an API at http://ip-api.com/ that will return latitude, longitude, and timezone for an IP address. The timezone is in the format "America/New_York" or the like, which you'll need moment.js and moment-timezone.js to turn into an actual hour-and-minute offset.

Keep in mind because of rules for Daylight Saving, the timezone offset for a given location isn't necessarily a constant -- that's why named timezones like "America/New_York" are used, and why you need something like moment.js to interpret the timezone offset for any particular time.

kshetline
  • 12,547
  • 4
  • 37
  • 73
0

You can use momentjs and get the user's zone like this:

const moment = require('moment-timezone');
let zone = moment.tz.guess()

Once you get the zone you can use it to get the timezone:

const tzn = moment().tz(zone).format('DD-MMM-YYYY HH:mm:ss Z');

For more clarification you can check momentjs docs: https://momentjs.com/timezone/docs/#/using-timezones/guessing-user-timezone/

General Grievance
  • 4,555
  • 31
  • 31
  • 45