Search code examples
angularjsangular-filters

formating date from controller not getting expected result


I am converting a string to date by using the angular-filter through controller. it's not getting expected result.

when i use the same in html that works fine.

here is my filter :

angular.module("tcpApp")
.filter("formatDate", 

    function () {

        return function (digiDate) {

            if(!digiDate) return;

            var regex = /\d+/g;

            return digiDate.match(regex)[0];

        }

});

my controller.js :

//weekDate is : Date(1438635600000+0300)/ - which i sending to fitler

    var weekDate =  $filter('formatDate')(contractorInfo.WeekDate, 'dd-MMM-yyyy');

    console.log( weekDate ); --> 1438635600000...!?

but in the html i am getting properly as 4-Aug-2014. how to handle the filter form controller?

what is wrong here?

Any one help me


Solution

  • Try sending a Date object instead of a unix timestamp which you are doing currently:

    var weekDate = new Date(contractorInfo.WeekDate);
    weekDate =  $filter('formatDate')(weekDate, 'dd-MMM-yyyy');