0

I have a variable that contains a date in epoch format:

var myDate = "1385923147000";

I can easily convert this in the latest FF, Chrome and IE10+ using:

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

Which displays as:

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

This fine in FF, Chrome and IE10+ however in IE8 this entire console.log/conversion is not executed which i causing my app to fail.

How can i convert in an IE8+ friendly way??

Sundar Rajan
  • 133
  • 1
  • 1
  • 12
Oam Psy
  • 8,555
  • 32
  • 93
  • 157

1 Answers1

1

Please see below

based on answer from here Date constructor returns NaN in IE, but works in Firefox and Chrome

you can do make it works like below:

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>
Community
  • 1
  • 1
sylwester
  • 16,498
  • 1
  • 25
  • 33