user1189352
user1189352

Reputation: 3885

Subtracting seconds with moment.js

I have a clock i want to start at 5:00. and I just want to make a button for instance that would subtract 5 seconds so that it displays 4:55. I'm having so much difficulty with this and JS DateTime. I keep getting the error "format is not a function". What am I missing?

var dateTime = new moment(300000);

var clock = dateTime.format('m:ss');  //Displays as 5:00

//Button Click
$scope.rewindClick = function () {
  clock = dateTime.diff(5000).format('m:ss');
}

Upvotes: 4

Views: 18121

Answers (2)

Marcelo Risse
Marcelo Risse

Reputation: 514

Use moment.js subtract http://momentjs.com/docs/#/manipulating/subtract/

dateTime.subtract(5, 'seconds').format('m:ss');

Upvotes: 14

Ashelyn Dawn
Ashelyn Dawn

Reputation: 1561

From the moment.js specs:

By default, moment#diff will return a number rounded towards zero (down for positive, up for negative).

So, dateTime.diff(5000) is returning a number, which has no format() function. As @marcelo-risse said, you'll want dateTime.subtract() instead.

Upvotes: 1

Related Questions