In angular we are provided with date $filter that formats date to a string based on the requested format.
There are several formats.
Some of them are :-
'medium': equivalent to 'MMM d, y h:mm:ss a' for en_US locale (e.g. Dec 4 201512:05:08 PM)
short': equivalent to 'M/d/yy h:mm a' for en_US locale (e.g. 8/5/15 12:05 PM)
fullDate': equivalent to 'EEEE, MMMM d, y' for en_US locale (e.g. Saturday, October 5, 2015)
longDate': equivalent to 'MMMM d, y' for en_US locale (e.g. October 6, 2015)
The syntax is as follows:-
$filter('date')(date, format, timezone)
Below is the example that shows the conversion of a date in format(2015-12-24 10:20:25) into long date format.
<ul>
<li class="photoimage1" ng-repeat="Img in Images">
<p style="font-size:13px;color: #999;margin-left:12px;margin-top:3px;margin-bottom:0;padding-bottom:10px;">Added {{Img.initial}}
</p>
</li>
<ul>
This is HTML where we want to display the date.
mainApp.controller("createnewController", function( $scope, $http, $timeout, $state, $rootScope, $filter ) {
var myData = {};
myData['responsetype'] = 'json';
$scope.date = new Date();
var responsePromise = $http.post( BASE_URL+"pro/mysample",JSON.stringify( myData ) );
responsePromise.success( function( data, status, headers, config ) {
console.log(JSON.stringify(data));
$scope.Images = data.data.myimages;
angular.forEach($scope.Images , function(Img){
console.log(Img);
Img.getFullDate = Img.created_at.split(" ");
Img.initial = $filter('date')(Img.getFullDate[0],'longDate');
console.log(Img.initial); });
} );
responsePromise.error( function ( data, status, headers, config ) {
console.log( JSON.stringify( data ) );
if(navigator.connection.type == Connection.NONE) {
checkConnection();
}
} );
In the above code I have injected the $filter into the controller and then splitted the date by space so that I get only the date and then used the format longDate to get the desired format and stored it into Img.initial and then displayed it into the view.
0 Comment(s)