user2456259
user2456259

Reputation: 157

Shortening a date with angular JS

I have a date coming across in this format: Tue, 05 Aug 2014 13:14:25 +0000

I need to trim the date so it reads Tue, 05 Aug 2014. I would like to trim off everything after the year.

I am trying to do this but in angular:

var date = item.pubDate;
var datelength = date.length;
date = date.substring(0, datelength - 14).trim();

Upvotes: 1

Views: 1211

Answers (1)

IUnknown
IUnknown

Reputation: 22468

In my opinion, the Angular way is to parse Date from string and apply date filter on it with format you want:

angular.module('myApp').controller('HomeController', ['$scope', '$filter',
  function($scope, $filter) {
    var pubDate= new Date('Tue, 05 Aug 2014 13:14:25 +0000');
    alert($filter('date')(pubDate, 'EEE, dd MMM yyyy'));
  }
]);

Or if you want to display this date on view, you can use filter in place:

angular.module('myApp').controller('HomeController', ['$scope',
  function($scope) {
    $scope.pubDate = new Date('Tue, 05 Aug 2014 13:14:25 +0000');
  }
]);

{{pubDate | date:'EEE, dd MMM yyyy'}}

Upvotes: 4

Related Questions