can someone help me out to write a function in react native to convert 234mins to 3hrs:54mins, and when it is less than 1hr, it shows just 59mins instead of 0hrs:59mins. Kindly indicate if I should give an example code.
Asked
Active
Viewed 2,337 times
-2
-
Have you looked into packages/libraries like moment.js or day.js? – Andrew Sep 11 '19 at 22:23
-
1Possible duplicate of [JavaScript seconds to time string with format hh:mm:ss](https://stackoverflow.com/questions/6312993/javascript-seconds-to-time-string-with-format-hhmmss) – codeteq Sep 11 '19 at 22:40
-
https://stackoverflow.com/questions/36035598/how-to-convert-minutes-to-hours-using-moment-js – Oleg Sep 12 '19 at 09:17
1 Answers
1
Here it is:
const convertMinsToTime = (mins) => {
let hours = Math.floor(mins / 60);
let minutes = mins % 60;
minutes = minutes < 10 ? '0' + minutes : minutes;
return `${hours}hrs:${minutes}mins`;
}

Maksym Bezruchko
- 1,223
- 6
- 23
-
many thanks, exactly what I needed, gracias. but I do not want the 0h:20mins if its less than 60, I just want 20mins – vincent O Sep 12 '19 at 10:59
-
change return to: `\`${hours ? \`${hours}hrs:\` : ''}${minutes}mins\``; – Maksym Bezruchko Sep 12 '19 at 11:21