How to get timezone offset as ±hh:mm in javascript?
Asked Answered
F

3

5

I can get the Timezone offset with the following command as:

new Date().getTimezoneOffset() -> -330 and using moment

moment().utcOffset() as 330.

However, how do I get the format as ±hh:mm?

Feeder answered 19/9, 2022 at 17:50 Comment(4)
the final time should be something like hh:mm:ss+|-hh:mmFeeder
What did you mean by that?can you show an expected format with times?Yb
yyyy-mm-dd hh:mm:ss+|-hh:mm. This is the full pictureFeeder
date and time format. e.g - 2019-01-01 12:32:45-08:00Feeder
Y
5

If you prefer moment.js you can use .format for formatting date or time.

 const date = new Date();
let utc = moment(date).utcOffset()
console.log(moment(date).utcOffset(utc).format("YYYY-MM-DD HH:mm:ss Z"))

//and for `+00:00` it will be : 
console.log('for GMT +0000 : '+ moment(date).tz('Africa/Monrovia').format('YYYY-MM-DD HH:mm:ss Z'))
<script src="https://momentjs.com/downloads/moment.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.23/moment-timezone-with-data-2012-2022.min.js"></script>

Reference : Display

Yb answered 19/9, 2022 at 17:58 Comment(8)
why in utcOffset(330) the argument is taken as 330. I want to make it genericFeeder
This doesn't provide in hh:mm:ss+|-hh:mm formatFeeder
You can store moment(date).utcOffset() in a variable and use it ?Yb
moment.utc(timestamp).utcOffset(moment(timestamp).utcOffset()).format("HH:MM:SSZ") the command will look something this way for timeFeeder
my conversionDateTime value must be in this format itself. yyyy-mm-dd hh:mm:ss+|-hh:mmFeeder
e.g - 2019-01-01 12:32:45-08:00Feeder
How to handle edge case where the time is in "Monrovia - Liberia" -> Tue Sep 20 2022 03:20:04 GMT+0000 and with the above command it returns 2022-09-20 03:20:04Z which is not in the correct format. expected is 2022-01-01 10:42:45+00:00 (can give + or - does not matter) this is google's conversion formatFeeder
@Feeder You can set the format for that. Try my updated answer. I think this is what you needed.Yb
P
3

The answer of QT Ray is a good point to start and helped me to solve the issue but actually has quite a few issues like trying to overwrite a numeric variable with a string or adding the minus in front of the string when the offset is positive.

Here is my working solution:

getTimezoneOffset(offset) {
  const offsetHours = Math.floor(Math.abs(offset / 60));
  const offsetMins = Math.abs(offset % 60);
  return (offset < 0 ? '-' : '+') + (offsetHours < 10 ? '0' : '') + offsetHours + ':' + (offsetMins < 10 ? '0' : '') + offsetMins;
}
Powder answered 30/1 at 16:6 Comment(0)
F
1

You won't be able to do it without a third-party library or manually converting the value with functionality.

You can do something like:

const offset = new Date().getTimezoneOffset();
let offsetHours = parseInt(Math.abs(offset/60));
let offsetMins = Math.abs(offset%60);
let formattedOffset;

if (offsetHours < 10) offSetHours = '0' + offsetHours;
if (offsetMins < 10) offsetMins = '0' + offsetMins;

if (offset < 0) {
  formattedOffset = '+' + offsetHours + ':' + offsetMins;
} else if (offset > 0) {
  formattedOffset = '-' + offsetHours + ':' + offsetMins;
} else if (offset === 0) {
 formattedOffset = 'Z';
}

That should get you pretty close.

Fultz answered 19/9, 2022 at 18:0 Comment(1)
parseInt will throw an error because it's numeric already, setting the offsetHours will do as well because you try to overwrite a number with a string, once it's offSetHours with a uppercase 'S' and I am pretty sure that the offset < 0 needs the minus upfront because that's when the offset is negative.Powder

© 2022 - 2024 — McMap. All rights reserved.