'Regex from Numbers to GMT

I'm trying to convert -2 to GMT Timezone format.

Or maybe its possible to find the GMT with JavaScript?

The result I need is:

  • from : -2
  • to : -02:00
date = new Date().getTimezoneOffset()/60
console.log(date)

==> -2 


Solution 1:[1]

This is how I solved this problem :

number1 = 5
number2 = -5
number3 = 11
number4 = -11
number5 = 0

const toGmt = (num) => {
  if ( (10 > num) && (num > 0) ) {
    return "(GMT+0"+num+":00)"
  } else if ( 10 <= num ) {
    return "(GMT+"+num+":00)" 
  } else if ( (0 > num) && (num > -10) ) {
    return "(GMT-0"+Math.abs(num)+":00)"
  } else if ( num <= -10 ){
    return "(GMT"+num+":00)"
  } else if ( num === 0 ){
    return "(GMT 0"+num+":00)"
  }
}

console.log(toGmt(number1)) ==> (GMT+05:00)
console.log(toGmt(number2)) ==> (GMT-05:00)
console.log(toGmt(number3)) ==> (GMT+11:00)
console.log(toGmt(number4)) ==> (GMT-11:00)
console.log(toGmt(number5)) ==> (GMT 00:00)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 pedrofromperu