new Date() in IE

最近在项目学,分到一个任务。后台获取的时间在firefox下面是没有问题的,但是在Ie下面显示NaN,纠结了很久,在网上找了很多解决方法,都不能搞定。

下面给出解决方案:


var endDate1 = $('#cycEndDate1').val();
var dateStr = new ExDate(endDate1).pattern("yyyy-MM-dd");
$('input[name="endDate"]').val(dateStr);

当然exDate.js 是必须要的。

以下是exDate.js 的内容ss


var ExDate = function(str){
    if(str.indexOf("GMT")!=-1){
        var strDate = str.replace(/Web/,'Wed');
        strDate = strDate.replace(/GMT/,'UTC');
        strDate = strDate.replace(/08:00/,'0800');
        return new Date(strDate);
    }else{
        return new Date(str);
    }
}
/**      
 * 对Date的扩展,将 Date 转化为指定格式的String      
 * 月(M)、日(d)、12小时(h)、24小时(H)、分(m)、秒(s)、周(E)、季度(q) 可以用 1-2 个占位符      
 * 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字)      
 * eg:      
 * (new Date()).pattern("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423      
 * (new Date()).pattern("yyyy-MM-dd E HH:mm:ss") ==> 2009-03-10 二 20:09:04      
 * (new Date()).pattern("yyyy-MM-dd EE hh:mm:ss") ==> 2009-03-10 周二 08:09:04      
 * (new Date()).pattern("yyyy-MM-dd EEE hh:mm:ss") ==> 2009-03-10 星期二 08:09:04      
 * (new Date()).pattern("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18      
 */        
Date.prototype.pattern=function(varFormat) {         
       var $format = varFormat || 'yyyy-M-d h:m:s'
    , $num = function(varn) { return (varn < 10 ? '0' : '') + varn; }
    , $yyyy = this.getFullYear(), $yy = $num($yyyy % 100)
    , $M = this.getMonth() + 1, $MM = $num($M)
    , $d = this.getDate(), $dd = $num($d)
    , $h = this.getHours(), $hh = $num($h)
    , $m = this.getMinutes(), $mm = $num($m)
    , $s = this.getSeconds(), $ss = $num($s);
    $format = $format.replace(/yyyy/g, $yyyy);
    $format = $format.replace(/yy/g, $yy);
    $format = $format.replace(/MM/g, $MM);
    $format = $format.replace(/M/g, $M);
    $format = $format.replace(/dd/g, $dd);
    $format = $format.replace(/d/g, $d);
    $format = $format.replace(/hh/g, $hh);
    $format = $format.replace(/HH/g, $hh);
    $format = $format.replace(/H/g, $h);
    $format = $format.replace(/h/g, $h);
    $format = $format.replace(/mm/g, $mm);
    $format = $format.replace(/m/g, $m);
    $format = $format.replace(/ss/g, $ss);
    $format = $format.replace(/s/g, $s);
    return $format;           


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值