How do I format data in a view in AngularJS?

Shamoon picture Shamoon · Nov 15, 2013 · Viewed 14.1k times · Source

<input type="text" ng-model="user.User.DateOfBirth"> is in my view. user.User.DateOfBirth is currently in YYYYMMDD format, but I want it to show as mm/dd/yyyy format in the view. Is it possible to convert this just for the display?

Answer

m.e.conroy picture m.e.conroy · Nov 15, 2013

This also may help you

http://jsfiddle.net/mikeeconroy/Hu9m2/1/

.filter('myFormatDateFilter',function(){
    return function(input){
        if(angular.isDefined(input)){
           if(input.length >= 8){
                input = input.slice(0,8);

                input = input.slice(4,6) + '/' + input.slice(6,8) + '/' + input.slice(0,4);
            }
        }
        return input;
    };
});