JavaScript:如何使用纯JavaScript将UTC日期时间转换为EST小时? [英] JavaScript : How do i convert UTC Date Time to EST Hours using pure Javascript?

查看:140
本文介绍了JavaScript:如何使用纯JavaScript将UTC日期时间转换为EST小时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将以非UTC合规格式获取输入时间格式为2016-04-10 9:00:00.0。当时我需要附加T,使其符合UTC。我也会获得时区,例如输入中的 - 5.00。有了这个细节,我需要将我的输入时间转换为 4 PM (EST时间,作为TimeZone传递)。

i will get input time format as "2016-04-10 9:00:00.0" in the non-UTC compliance format. I need to append the "T" in the time to make it compliance with the UTC. I will also get the timezone as eg "-5.00" in the input. With this details i need to convert my input time to 4 PM (EST time which was passed as TimeZone).

我可以不要使用任何第三方库。

I can't use any third party library.

HTML

<h1>
TimeZone
</h1>
<h2 id="hourValue">

</h2>
<table>
<tr>
  <td>Date Time</td>
  <td><input type="text" id="txtDate" value="2016-04-10  09:00:00.0" /></td>
</tr>
<tr><td>TimeZone</td></tr>
<tr><td><input type="text" id="txtOffset" value="-5.00" /></td></tr>
<tr><td><input type="submit" id="btnSubmit" value="Convert" onClick="myTime()" /></td></tr>
</table>

JavaScript
function myTime()
{

var d1= document.getElementById("txtDate").value;
var zOffset = document.getElementById("txtOffset").value;
console.log("Date1",d1);

var d2 = new Date(d1.replace(/ /g,'T'));

var d3= d2.getTime()+(d2.getTimezoneOffset()*60000);

console.log("Date2",d2);
console.log("Date3",d3);
var d4 = new Date(d3 + (3600000 * zOffset));

console.log("Date3",d3);
console.log("Date 4",d4);

var d5 = d4.toLocaleString();
console.log("Date 5",d5);

d6 =d5.match(/(\d+)(?=:\d+:\d+)|([A-Z]+)(?=$)/g).join(" ");

console.log("Date6",d6)

document.getElementById("hourValue").innerHTML = d6.value;
}

我在评论区域中提供了jsfiddle链接,小提琴可以正常工作FF而不是IE。这是我的问题。

I have given jsfiddle link below in the comments area, the fiddle works fine in FF and not in IE. That's my problem.

推荐答案

您不应该使用Date构造函数解析字符串,这是不可靠的,在很大程度上取决于实现。 IE 8将不会像字符串一样解析ISO 8601。

You should not parse strings with the Date constructor, it's unreliable and largely implementation dependent. IE 8 won't parse ISO 8601 like strings.

可以通过获取数字和数字轻松解析一个字符串,如2016-04-10 9:00:00.0将它们传递给Date构造函数。然后应用偏移也是直接的(你在做什么是好的,但如果你喜欢,它可以包含在解析函数中),请参阅下面的功能。 IE 8中的工作也可以在其他地方工作。

A string like "2016-04-10 9:00:00.0" can be easily parsed by getting the numbers and passing them to the Date constructor. To then apply an offset is also straight forward (what you're doing is OK but it can be included in the parse function if you like), see function below. What works in IE 8 will also work everywhere else.

我假设您的偏移量为±h.mm,但可能为小数点,在这种情况下只需修改偏移部分(或单独应用)。我将偏移量作为分钟直接应用于分钟值而不是时间值。

I've assumed that your offset is ±h.mm but it may be decimal hours, in that case just modify the offset part (or apply it separately). I apply the offset as minutes directly to the minutes value rather than the time value.

但是,任何此类函数都应验证值并返回相应的错误消息(如果它们失败)。

However, any such functions should validate the values and return appropriate error messages if they fail.

// Date string like: 2016-04-10  09:00:00.0
// Offset like: -5.00  assuming h.mm
function parseString(dateString, offset) {
  var b = dateString.split(/\D+/);
  var o = (offset < 0? -1 : 1) * 
          (Math.abs(parseInt(offset)*60) + +(offset.replace(/^.*\.(\d+)$/,'$1')));
  return new Date(Date.UTC(b[0], b[1]-1, b[2], b[3], +b[4]-o,
                           b[5], (b[6]+'00').slice(0,3)));
}

// Sample values
var dateString = '2016-04-10  09:00:00.0'
var offset = '-5.00';
var d = parseString(dateString, offset);
document.write(
'<br>Time  : ' + dateString +
'<br>Offset: ' + offset + 
'<br>Local : ' + d);

body {
  font-family: courier, monospace;
  white-space: pre;
  }

这篇关于JavaScript:如何使用纯JavaScript将UTC日期时间转换为EST小时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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