日期在IE

时间:2016-12-19 09:27:07

标签: javascript internet-explorer cross-browser

我尝试将UTC时间字符串转换为本地时间。在我的功能中,sate在IE中重新调整了Nan,但是在FF& Chorome。

var myDate = '2016-12-17 4:20';
var LocDate = convertUTCDateToLocalDate(new Date(myDate));
console.log(LocDate);

输出(IE):NaN-aN-aN aN:aN

function convertUTCDateToLocalDate(date) {
  if(typeof date !== undefined){
    var locDate,iYear,iMonth,iDay,iMin;
    var newDate = new Date(date);
    newDate.setMinutes(date.getMinutes() - date.getTimezoneOffset());
    iYear = newDate.getFullYear();
    iMonth = newDate.getMonth() + 1;
    iDay = newDate.getDate();
    iHrs = newDate.getHours();
    iMin = newDate.getMinutes();
    locDate = iYear + '-' + ('0' + iMonth).slice( - 2) + '-' + ('0' + iDay).slice( - 2) + ' ' + ('0' + iHrs).slice( - 2) + ':' + ('0' + iMin).slice( - 2);
    return locDate;
  }
}

如何修复IE?

0 个答案:

没有答案