AngularJS - 日期不在IE8中转换

时间:2014-11-04 10:54:08

标签: javascript jquery angularjs internet-explorer-8 angularjs-scope

我有一个包含epoch格式日期的变量:

var myDate = "1385923147000";

我可以使用以下方法在最新的FF,Chrome和IE10 +中轻松转换它:

myDate = new Date($filter('date')(myDate, 'yyyy-MM-dd'));
myDate.setHours(0,0,0,0);
console.log(myDate);

显示为:

Sun Dec 01 2013 00:00:00 GMT+0000 (GMT Standard Time)

在FF,Chrome和IE10 +中都很好但是在IE8中这个整个console.log /转换没有执行,导致我的应用程序失败。

我如何以IE8 +友好的方式转换?

1 个答案:

答案 0 :(得分:1)

请参阅下文

基于此处的回答Date constructor returns NaN in IE, but works in Firefox and Chrome

你可以让它像下面这样工作:

var app = angular.module('app', []);

app.controller('homeCtrl', function($scope, $log, $filter) {

  
 function parseISO8601(dateStringInRange)
    {
        var isoExp = /^\s*(\d{4})-(\d\d)-(\d\d)\s*$/,
            date = new Date(NaN), month,
            parts = isoExp.exec(dateStringInRange);

        if (parts) {
            month = +parts[2];
            date.setFullYear(parts[1], month - 1, parts[3]);
            if (month != date.getMonth() + 1) {
                date.setTime(NaN);
            }
        }
        return date;
    }
  

 var myDate = "1385923147000";

   
  myDate = ($filter('date')(myDate, 'yyyy-MM-dd'));
  $scope.myDate = parseISO8601(myDate);
  $scope.myDate.setHours(0, 0, 0, 0);
  console.log($scope.myDate);


});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="app">
  <div ng-controller="homeCtrl">

    Date : {{myDate}}
  </div>
</div>