2

I need to get the time zone of where the user is logged in from. I know userinfo can get the time zone of the user but i want the timezone of the device currently logged in. If my traveling user works out of Chicago, but is putting in time in New York. I want to know what time it is where he physically is at that moment.

Is that possible in apex?

Thanks!

1

1 Answer 1

4

I don't believe this is possible with Apex since it runs on the server it has limited access to the client's browser and configuration. However you can access this with standard browser APIs and some Javascript using Intl.DateTimeFormat.prototype.resolvedOptions() which will give you the IANA timezone name. There are also ways of accessing the timezone offset using Date.getTimezoneOffset().

let myTimezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
console.log(myTimezone); //America/New_York

let d = new Date(); //instantiates as current date & time
console.log(d.getTimezoneOffset()); //returns the offset, in minutes. 
// So -60 => UTC+1, 480 => UTC-8

Bear in mind there's a chance that a user's browser is misconfigured and the time on their machine is not correct.

1
  • Alas client side is not really an option here because the users could be using the browser or FSL to record activities. That's why i was hoping for an apex solution. I'm aware of the potential issues of browser misconfigurations or even cell tower problems. Thank you for the advice!
    – johnlxc
    Commented Sep 10, 2020 at 21:58

Not the answer you're looking for? Browse other questions tagged .