I currently am using the library dayjs in an application for time tracking. Each user tracks the number of minutes they have worked on an assignment and the total number of minutes worked is stored as an integer. For example, a user who works 2 hours in total will have the total number of minutes worked stored as 120
. The result I am trying to produce, is if a user has 120
hours worked I would like to display that back to the user as 02:00
. I have achieved this using the following method:
const totalMinutes = 120
const dateObject = dayjs.duration(totalMinutes, 'minutes').$d
const hoursToString = dateObject.hours.toString()
const minToString = dateObject.minutes.toString()
const hours =
hoursToString.length <= 1
? '0' + hoursToString
: dateObject.hours.toString()
const minutes = minToString.length <= 1 ? '0' + minToString : minToString
const finalTime = hours + ':' + minutes
while this method works, I can't help but feel like there's a much more efficient way of tackling this. Attached is a code sandbox for debugging https://codesandbox.io/s/nervous-chatelet-s6vxz?file=/src/App.js any and all suggestions are welcomed!