Reputation: 6684
I have this code:
<body ng-app="my-app" ng-controller="my-controller">
<h1>Empty Angular App</h1>
<span>{{"2015-07-08T15:10:10.530Z" | date:'medium'}}</span><br>
<span>Expected: Aug 23, 2015 10:10:10 PM</span>
</body>
http://codepen.io/vicheanak/pen/MwVNwG/
I'd like to add 46 days on top of "2015-07-08T15:10:10.530Z". Expected result is: Aug 23, 2015 10:10:10 PM
Thanks for your help
Upvotes: 3
Views: 31476
Reputation: 6215
You could create a custom filter to convert the date from the format ISO8601 and add X days:
myApp.filter('kDateAddFromDateISO8601', [function() {
return function(isoDateString, days) {
var parts;
var isoTime;
var date;
isoDateString = isoDateString || "";
days = days || 0;
parts = isoDateString.match(/\d+/g);
isoTime = Date.UTC(parts[0], parts[1] - 1, parts[2], parts[3], parts[4], parts[5]);
date = new Date(isoTime);
if (days) {
date.setDate(date.getDate() + days);
}
return date;
};
}]);
And then you can use the filter:
<div>{{"2015-07-08T15:10:10.530Z" | kDateAddFromDateISO8601 : 46 | date:'medium'}}</div>
Output:
Aug 23, 2015 12:10:10 PM
Upvotes: 2
Reputation: 661
As you told, use javascript is better option.
var app = angular.module('my-app', []);
app.controller("my-controller", function($scope) {
$scope.name = "world";
$scope.mydate = new Date("2015-07-08T15:10:10.530Z");
var numberOfDaysToAdd = 46;
$scope.newdate = $scope.mydate.setDate($scope.mydate.getDate() + numberOfDaysToAdd);
});
http://codepen.io/anon/pen/MwVNpq
Upvotes: 9