AngularJS Filter Change Date Format in Controller Example
In this post, i will show you how to change date format in angularjs controller. if you need to change date format in html then you can use date filter, but in controller you need to change date formate like yyyy-mm-dd to dd/MM/yyyy than how you can do it.
you can change date formate using $filter('date')(date format timezone) in angular js controller.
Some days ago, someone ask me in comment. can you suggest me to change date formate in angular controller. he has date string and he wanted to change formate. so you don't require to convert string, but you can do it directly by using $filter in your controller.
Just see this example and code.
Example:
<!doctype html>
<html>
<head>
<title>Angularjs filter change date format in controller - ItSolutionStuff.com</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
</head>
<body ng-app='myapp'>
<div ng-controller="myCtrl">
<p>2019-02-13</p>
<p><strong>{{ item }}</strong></p>
</div>
</body>
<script type="text/javascript">
var app = angular.module('myapp', []);
app.controller('myCtrl', ['$scope', '$http', '$filter', function ($scope, $http, $filter) {
$scope.item = $filter('date')('2019-02-13', "dd/MM/yyyy");
}]);
</script>
</html>
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- AngularJS Scroll to a Specific Element using Anchorscroll
- AngularJS Convert Comma Separated String to Array Example
- AngularJS Nl2br Filter for Textarea Content Example
- AngularJS Display Default Image If Original Does Not Exists Example
- AngularJS Update Bind Ng-model Input Value from JQuery Example
- How to Call AngularJS Controller Function in JQuery?
- How to Remove # from URL in AngularJS?
- AngularJS Image Upload with Preview Example
- How to Hide Div After Some Time in AngularJS?
- How to Call Function on Page Load in AngularJS?
- How to Change Date Format using Date Filter in AngularJS?
- AngularJS - How to Limit String Length using Filter?
- AngularJS - How to Capitalize the First Letter of Word Example
- How to Remove HTML Tags from String in AngularJS?