-1

How do you check according to a timezone if you should modify the UTC time to -1 or +1 in Javascript? Need a function that takes in a timezone as string, and a time in UTC format and either add -1 or +1 depending on whether according to the timezone there's daylight savings or not and modify the UTC time appropriately. Is there a library that does this?

Sayaman
  • 1,145
  • 4
  • 14
  • 35
  • How is the timezone defined? IANA timezones identified by representative locations may change offset depending on historic changes and daylight saving so the date and time, either local or UTC, is important. Other timezones do not. Typically the definition of the offset includes a sign to indicate whether it should be added or subtracted. Common offsets are positive for east of Greenwich and negative for west, POSIX offsets (such as ECMAScript's *Date.prototype.getTimezoneOffset*) are the opposite. – RobG Aug 30 '22 at 20:56
  • https://en.wikipedia.org/wiki/List_of_tz_database_time_zones these timezones are used. – Sayaman Aug 30 '22 at 22:58
  • 2
    In that case you need to also supply a date and time. Daylight saving offsets typically change an hour or two after midnight, so the changeover day will have two different offsets depending on the time. Likely a duplicate of [*how to get time zone of city or country in javascript*](https://stackoverflow.com/questions/37001869/how-to-get-time-zone-of-city-or-country-in-javascript). – RobG Aug 31 '22 at 03:14
  • 1
    Also see [*How to initialize a JavaScript Date to a particular time zone*](https://stackoverflow.com/questions/15141762/how-to-initialize-a-javascript-date-to-a-particular-time-zone). – RobG Aug 31 '22 at 03:19
  • 2
    To be clear - you should *never* add -1 or +1 yourself, regardless of language. – Matt Johnson-Pint Aug 31 '22 at 03:57

1 Answers1

3

How do you check according to a timezone if you should modify the UTC time to -1 or +1 in Javascript?

For common offsets, the sign of the offset tells you whether to add or subtract from UTC to get local. So GMT+5:30 means add 5 hours and 30 minutes to UTC to get local.

POSIX offsets (such as those generated by ECMAScript's Date.prototype.getOffset) are the opposite, i.e. they indicate the time to add or subtract from local to get UTC, so in the above case the offset would be -5:30.

Need a function that takes in a timezone as string, and a time in UTC format and either add -1 or +1 depending on whether according to the timezone there's daylight savings or not and modify the UTC time appropriately.

Not all daylight saving offsets are 1 hour, some are 30 minutes. Typically places have a timezone name ending in "standard time" to indicate the normal offset, and a "daylight saving time" or "summer time" for when the daylight saving offset applies.

Offsets also depend on the date, so you need to provide a date and time like "2022-08-31T08:30:00Z".

Is there a library that does this?

Yes, there are many however library recommendations are off topic here. Research them, have a play, pick one you like. Then ask questions about a specific issue and library if you have any.

But you can also use POJS with either Date.prototype.toLocaleString or Intl.DatetimeFormat with suitable options and parsing the resulting string. You might also use Intl.DateTimeFormat.prototype.formatToParts:

let date = '2022-08-31T08:30:00Z';
let loc = 'Asia/Kolkata';

console.log(new Date(date).toLocaleString('en',{
  hour: 'numeric',
  hour12: false,
  timeZone: loc,
  timeZoneName: 'short'
}));

console.log( 
new Date('2022-08-31T12:00:00Z').toLocaleString('en',{hour:'numeric', hour12:false, timeZone:'Asia/Kolkata', timeZoneName:'short'})
);

Note that the Intl.DateTimeFormat has numerous options for timeZoneName such as shortOffset that aren't widely supported yet so use with caution and stick with older options for wider compatibility.

RobG
  • 142,382
  • 31
  • 172
  • 209