AngularJS - Convert Dates In Controller


Answer :

item.date = $filter('date')(item.date, "dd/MM/yyyy"); // for conversion to string 

http://docs.angularjs.org/api/ng.filter:date

But if you are using HTML5 type="date" then the ISO format yyyy-MM-dd MUST be used.

item.dateAsString = $filter('date')(item.date, "yyyy-MM-dd");  // for type="date" binding   <input type="date" ng-model="item.dateAsString" value="{{ item.dateAsString }}" pattern="dd/MM/YYYY"/> 

http://www.w3.org/TR/html-markup/input.date.html

NOTE: use of pattern="" with type="date" looks non-standard, but it appears to work in the expected way in Chrome 31.


create a filter.js and you can make this as reusable

angular.module('yourmodule').filter('date', function($filter) {     return function(input)     {         if(input == null){ return ""; }         var _date = $filter('date')(new Date(input), 'dd/MM/yyyy');         return _date.toUpperCase();     }; }); 

view

<span>{{ d.time | date }}</span> 

or in controller

var filterdatetime = $filter('date')( yourdate ); 

Date filtering and formatting in Angular js.


Comments

Popular posts from this blog

Chemistry - Bond Angles In NH3 And NCl3

Are Regular VACUUM ANALYZE Still Recommended Under 9.1?

Change The Font Size Of Visual Studio Solution Explorer