David.titan David.titan - 4 months ago 54
AngularJS Question

AngulaJs Date format on controller

I have this code and it returns a nice date format in the

html
part but I need to return the same format in the
JavaScript
"controller" part.

function Ctrl($scope)
{
$scope.date = new Date();

}


view:

<div ng-app ng-controller="Ctrl">
{{date | date:'yyyy-MM-dd HH:mm:ss'}}

</div>


Return:
2016-07-09 00:14:09


So I want to return the same format in
console.log();

Any help will be appreciated.

SSH SSH
Answer

Try use $filter service in controller like this. you should inject $filter service in controller.

  app.controller("Ctrl",function ($scope, $filter){
  });

in your syntax controller

  function Ctrl($scope, $filter)
   {
     $scope.date = new Date();
   }

 var date  = $filter('date')(new Date(input), 'yyyy-MM-dd HH:mm:ss');

// Code goes here

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


app.controller("Ctrl",function($scope,$filter){
  
   $scope.date = new Date();
  var date  = $filter('date')($scope.date, 'yyyy-MM-dd HH:mm:ss');
  
  console.log(date);
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller=" Ctrl">
 {{date | date:'yyyy-MM-dd HH:mm:ss'}}

</div>

Comments