在IE8中返回NAN的Javascript日期 [英] Javascript date returning NAN in IE8

查看:112
本文介绍了在IE8中返回NAN的Javascript日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解析日期,例如 2012-12-07T16:18:15 + 05:30 我以字符串格式从数据库接收这些日期。
我正在使用的解析函数是:

I am trying to parse date like this 2012-12-07T16:18:15+05:30 which I am receiving from database in string format. The parse function I am using is:

var jstime = new Date("2012-12-07T16:18:15+05:30"); 
 var h = jstime.getHours();
var m = jstime.getMinutes();
var s = jstime.getSeconds();
var f = "am"
if(h >= 12)
{
    f = "pm";
    h = h - 12;
}
if(h == 0)
{
    h = 12;
}
var str;
str = jstime.toDateString();
str = str +"," + h.toString() + ":" + m.toString() + ":" + s.toString() + " " + f.toString(); 

然而, IE8浏览器在第一行返回NAN ,即jstime是NAN在IE8中,在其他浏览器中工作正常。

那么,有没有其他方法可以解析在所有浏览器中运行良好的日期?

我需要它以上述格式接受日期&返回日期格式:
Fri Dec 07 2012,4:18:15 pm

However,IE8 browser returning NAN at very first line i.e. jstime is NAN in IE8,while working fine in other browsers.
so, Is there any alternate way to parse date that works well in all browsers?
I need it accepts date in above format & returns date in format: Fri Dec 07 2012,4:18:15 pm?

推荐答案

如果你能确定格式,可以正则表示:

If you can be sure of the format, you can regex it:

var match = "2012-12-07T16:18:15+05:30".match(/(\d{4})-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)([+-])(\d\d):(\d\d)/);
var jstime = new Date();
jstime.setUTCFullYear(parseInt(match[1],10));
jstime.setUTCMonth(parseInt(match[2],10)-1);
jstime.setUTCDate(parseInt(match[3],10));
jstime.setUTCHours(parseInt(match[4],10)-parseInt(match[7]+"1",10)*parseInt(match[8],10));
jstime.setUTCMinutes(parseInt(match[5],10)-parseInt(match[7]+"1",10)*parseInt(match[9],10));
jstime.setUTCSeconds(parseInt(match[6],10));

但是如果它来自服务器端,你可以在那里更可靠地格式化它。

But if it's coming from the server-side, you may be able to format it more reliably there.

这篇关于在IE8中返回NAN的Javascript日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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