formatDuration for hours and minutes

I’ve gotten myself thoroughly confused with the ‘formatDuration’ syntax, hoping someone has done this before.

I have time in minutes that I’d like to convert to hours and minutes, for example:
202 (minutes) = 3:22 (3 hours and 22 minutes). I want to format it as 3:22.

After reading the date formatting page, I thought I was all set – but there must be something I’m doing wrong between the millis and minutes.

Here’s what I’m doing:

minutes = $ // the value is '202'
duration = durationAs(minutes, "minutes")
time = formatDuration(duration, "HH:mm")

what I get back is 00:00.


Keep in mind the inputs and outputs of each function. Most date functions expect a value in milliseconds.

durationAs(millis, units) #// output: numeric representation of the milliseconds in your choice of units
formatDuration(millis, format) #// output: a formatted string

So in both cases, each function is expecting the number of milliseconds as the primary input.


In the case of durationAs(), it outputs the duration as whatever 'units' you specify in the second parameter. So giving it 202 milliseconds and asking it how many minutes that is results in 0.

Since it sounds like your value is already in minutes, you don’t need this function. If you did need to convert from one unit to another, here’s some examples:

minutes = 202 
millis = minutes * 60 * 1000 #// 60 seconds in each minute, 1000 milliseconds in each second

durationAs(millis, 'seconds') #// 12,120 (seconds)
durationAs(millis, 'minutes') #// 202 (minutes)
durationAs(millis, 'hours')   #// 3.366 (hours)


This takes an input in milliseconds along with a format string and converts the duration into your format string. In your case, the duration from the previous calculation was 0 based on the logical error noted above. So when you gave it 0 milliseconds, the duration string it output was also 0.

minutes = 202 
millis = minutes * 60 * 1000 #// 60 seconds in each minute, 1000 milliseconds in each second

formatDuration(millis, "HH:mm") #// 03:22
formatDuration(millis, "H:mm")  #// 3:22
formatDuration(millis, "H")     #// 3
formatDuration(millis, "m")     #// 22
1 Like