最近在项目学,分到一个任务。后台获取的时间在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;
}