javascript - Convert timezone offset number of hours to timezone offset in (military?) hours - Stack Overflow

I am using WordPress and jQuery Timepicker.Wordpress gives timezone offset in hours: like +2 or -3.5jQu

I am using WordPress and jQuery Timepicker.

Wordpress gives timezone offset in hours: like +2 or -3.5

jQuery Timepicker takes offset in military hours: +0200 or -0330

I am currently using something like this:

gmt_offset = -4;
hrs_offset = gmt_offset.replace('-', '-0') + "00";

= -0400

But that will break if the offset is not a single negative digit.

gmt_offset = -3.5;
hrs_offset = gmt_offset.replace('-', '-0') + "00";

= -03.500  //No good...

need: -0330

Ack! Can someone help me figure this out?

I am using WordPress and jQuery Timepicker.

Wordpress gives timezone offset in hours: like +2 or -3.5

jQuery Timepicker takes offset in military hours: +0200 or -0330

I am currently using something like this:

gmt_offset = -4;
hrs_offset = gmt_offset.replace('-', '-0') + "00";

= -0400

But that will break if the offset is not a single negative digit.

gmt_offset = -3.5;
hrs_offset = gmt_offset.replace('-', '-0') + "00";

= -03.500  //No good...

need: -0330

Ack! Can someone help me figure this out?

Share Improve this question asked Mar 28, 2013 at 17:03 DigitalDesignDjDigitalDesignDj 1,8951 gold badge16 silver badges16 bronze badges 2
  • It will also break if you try "-11" – Derek 朕會功夫 Commented Mar 28, 2013 at 17:16
  • Indeed, -11 is not a single negative digit – DigitalDesignDj Commented Mar 28, 2013 at 17:50
Add a ment  | 

3 Answers 3

Reset to default 3

Split the offset into 2 parts using .split(".").

Give the hour part a "0" if it is less than 10. Then append a negative sign if it is originally negative.

var negative = hour < 0 ? true : false;
hour = Math.abs(hour) < 10 ? "0" + Math.abs(hour) : Math.abs(hour);
hour = negative ? "-" + hour : "+" + hour;

To calculate the minutes part, multiply it by 6.

(time[1]*6).toString()

Here's the final function:

function convertOffset(gmt_offset) {
    var time = gmt_offset.toString().split(".");
    var hour = parseInt(time[0]);
    var negative = hour < 0 ? true : false;
    hour = Math.abs(hour) < 10 ? "0" + Math.abs(hour) : Math.abs(hour);
    hour = negative ? "-" + hour : "+" + hour;
    return time[1] ? hour+(time[1]*6).toString() : hour + "00";
}
document.write(convertOffset(-3.5));

See DEMO.

function convert(gmt_offset) {
     var sign ="+";
     if(gmt_offset < 0) {
         sign="-";
         gmt_offset *= -1;
     }
     var hours = "0"+Math.floor(gmt_offset).toString();
     var minutes = "0"+(Math.round(gmt_offset % 1 * 60)).toString();
     return sign + hours.substr(hours.length - 2) + minutes.substr(minutes.length - 2);
}

Here's a nicely pact version:

function militaryGMTOffsetFromNumeric(offset) {
   var dt = new Date(
      Math.abs(offset) * 3600000 + new Date(2000, 0).getTime()
   ).toTimeString();
   return (offset < 0 ? '-' : '+') + dt.substr(0,2) + dt.substr(3,2);
}

For example, militaryGMTOffsetFromNumeric(-3.5) returns -0330.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745195787a4616080.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信