-2

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.

vincent O
  • 518
  • 8
  • 33
  • Have you looked into packages/libraries like moment.js or day.js? – Andrew Sep 11 '19 at 22:23
  • 1
    Possible 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 Answers1

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