I have a system with users spread all over the world and assigned to a couple of geographic organizational units – basically, each unit has a time zone assigned, usually one that matches the time zone for unit’s HQ location. To handle different time zones everything is done using UTC dates and times in both, backend and front-end.
At the moment I’m looking to refactor the front end to show and manipulate datetimes using the local time zone of the unit. A lot of the users are working remote or have access to multiple units so the time zone of their system might not match the unit time zone.
So the basic question here is what is the proper way to store and manipulate the date times in front-end to be able to save the data back in database as UTC and then read it back and displayed as the local time of the unit.
I played a little bit with JS libraries like Luxon or Moment Timezone and it seems they can do the job but I was wondering if it can be done using only the browser’s API like Date object or Intl namespace object.
I read a lot of posts on StackOverflow that tackle creating a date time in a different time zone but I didn’t find an accepted solution for a problem like mine. In pseudo-code my problem will resume to this:
/**
* @param string sessionStartTime (represents the UTC date formatted as ISO string (eg. 2022-04-27T05:06:07.000)
* @param string timezone (eg. America/Los_Angeles)
* @param integer minutesToAdd (eg. 45)
*/
function getSessionEndDateTime(sessionStartTime, timezone, minutesToAdd) {
// Some processing - create a new Date object, add minutes, format the output
return localDateTimeFormatted;
}