Why the following code produces strange output (looks like moment().valueOf() returns 0)
Since 7 days ago : {{(moment().valueOf() - 7*24*60*60*1000) | date:'yyyy-MM-dd' }}
returns
Since 7 days ago : 1969-12-25
You can do it with moment API :
moment().subtract('days', 7).format("YYYY-MM-DD")
Working jsfiddle : http://jsfiddle.net/D9UCF/1/
This is because:
moment#valueOf simply outputs the number of milliseconds since the Unix Epoch, just like Date#valueOf.
One way to achieve what you want is as follows:
Since 7 days ago: <span ng-bind="sevenDaysAgo"></span>
$scope.sevenDaysAgo = moment(new Date(new Date().setDate(new Date().getDate() - 7))).format('YYYY-MM-DD');
moment(moment().valueOf() - 7*24*60*60*1000).format('YYYY-MM-DD'). You can bind this directly, without using AngularJSdateFilter. EDIT: Bixi's answer is definitely clearer.