原代码:
// 格式化时间戳 yyyy-MM-dd hh:mm:ss, type==1: yyyy-MM-dd ; type==2: yyyy-MM-dd hh:mm:ss
function getDateStr(my_dateTime, type) {
if (isBlank(my_dateTime)) {
return '';
}
var d = new Date(my_dateTime);
var year = d.getFullYear(),
month = d.getMonth()+1,
date = d.getDate(),
hour = d.getHours(),
minute = d.getMinutes(),
second = d.getSeconds();
function p(s) {
return s<10 ? ('0'+s) : s;
}
var timeFormat = year + "-" + p(month) + "-" + p(date);
if (type === 2) {
timeFormat = timeFormat + ' ' + p(hour) + ':' + p(minute);
}
if (type === 3) {
timeFormat = timeFormat + ' ' + p(hour) + ':' + p(minute) + ':' + p(second);
}
return timeFormat;
}
在其他浏览器都OK 有的时间到了IE就成了NAN-NAN-NAN NAN:NAN
一串NAN。。。
更改后的代码
// 格式化时间戳 yyyy-MM-dd hh:mm:ss, type==1: yyyy-MM-dd ; type==2: yyyy-MM-dd hh:mm:ss
function getDateStr(my_dateTime, type) {
if (isBlank(my_dateTime)) {
return '';
}
if(typeof my_dateTime == "string"){
my_dateTime = new Date(my_dateTime.replace(/-/g,"/"));
}
var d = new Date(my_dateTime);
var year = d.getFullYear(),
month = d.getMonth()+1,
date = d.getDate(),
hour = d.getHours(),
minute = d.getMinutes(),
second = d.getSeconds();
function p(s) {
return s<10 ? ('0'+s) : s;
}
var timeFormat = year + "-" + p(month) + "-" + p(date);
if (type === 2) {
timeFormat = timeFormat + ' ' + p(hour) + ':' + p(minute);
}
if (type === 3) {
timeFormat = timeFormat + ' ' + p(hour) + ':' + p(minute) + ':' + p(second);
}
return timeFormat;
}
增加了一个判断 将字符串类型转换成全浏览器都支持的时间格式
if(typeof my_dateTime == "string"){
my_dateTime = new Date(my_dateTime.replace(/-/g,"/"));
}