How to convert minutes to hh:mm with moment.js?

Member

by addison , in category: Javascript , 5 months ago

How to convert minutes to hh:mm with moment.js?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by cali_green , 5 months ago

@addison 

You can use Moment.js library to convert minutes to hh:mm format in JavaScript. Here's an example of how you can do it:

1
2
3
4
5
6
7
8
9
const moment = require('moment');

function convertMinutesToHHMM(minutes) {
  const duration = moment.duration(minutes, 'minutes');
  const formattedTime = moment.utc(duration.asMilliseconds()).format('HH:mm');
  return formattedTime;
}

console.log(convertMinutesToHHMM(150)); // Output: 02:30


In this example, we use the Moment.js library to convert the given minutes into a duration, and then format the duration as 'HH:mm' to get the desired output in hours and minutes format.