将时区偏移量小时数转换为以(军用小时)小时为单位的时区偏移量 [英] Convert timezone offset number of hours to timezone offset in (military?) hours

查看:159
本文介绍了将时区偏移量小时数转换为以(军用小时)小时为单位的时区偏移量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用WordPress和jQuery Timepicker.

I am using WordPress and jQuery Timepicker.

WordPress会以小时为单位提供时区偏移量,例如+2或-3.5

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

jQuery Timepicker的军事用时发生了偏移:+0200或-0330

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

A!有人可以帮我解决这个问题吗?

Ack! Can someone help me figure this out?

推荐答案

使用.split(".")将偏移量分成两部分.

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

如果小时部分小于10,则将其设为"0".如果最初为负,则添加一个负号.

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;

要计算分钟部分,请乘以6.

To calculate the minutes part, multiply it by 6.

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

这是最后一个功能:

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));

请参见演示.

这篇关于将时区偏移量小时数转换为以(军用小时)小时为单位的时区偏移量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆