user3214545
user3214545

Reputation: 2213

MomentsJS asHours formatting

I have a result in milliseconds and I am trying to display it as Hours and Minutes using Momentjs.

var x = 161545000;
var tempResult = moment.duration(x);

var formattedX = tempResult.asHours() + 'h ' + tempResults.minutes() + 'm ';

What is being displayed:

2.2701202777777776h 16m

What I want, but can't seem to format it right:

2h 16m

Upvotes: 2

Views: 5894

Answers (1)

mingos
mingos

Reputation: 24502

How about using Math.floor()?

var formattedX = Math.floor(tempResult.asHours()) + 'h ' +
    Math.floor(tempResults.minutes()) + 'm ';

Or, better yet, use the Moment#hours() method instead of Moment#asHours():

var formattedX = tempResult.hours() + 'h ' + tempResults.minutes() + 'm ';

Upvotes: 6

Related Questions