Convert Duration to string

Duration fields are awesomely useful. However, if one wants to simply transfer the displayed duration (e.g. 1:45) to another field via a formula, Airtable passes along the stored value in seconds. Converting that back into a duration-like display is doable, but a little tricky, as I found out in this recent discussion:

I tried the TIMESTR() function, but that just gave an error, so I ended up writing my own seconds-to-duration formula.

My suggestion: expand the TIMESTR() function’s capabilities to also convert durations into strings. I suppose a new function named something like DURSTR() might also work, but because durations are all about time, wrapping that conversion into TIMESTR() feels like it makes more sense.

3 Likes

Very much agreed.

As a workaround, I tried a formula too. I’m sure there is a simpler version but this seemed OK.

ROUNDDOWN({Duration}/3600, 0)
& “:” & IF(MOD({Duration}/60,60)<10,“0”&MOD({Duration}/60,60),MOD(Duration}/60,60))

Good job on the formula. Looking back at when I wrote the initial post, that must’ve been really really close to when I first started using Airtable. Nowadays I’d do pretty much what you wrote. :slight_smile:

1 Like

Took the base of this formula and made something that looks a little cleaner:

IF(ROUND(MOD({Duration}/60,60))<10,ROUNDDOWN({Duration}/3600,0)&":"&“0”&ROUND(MOD({Duration}/60,60)),IF(ROUND(MOD({Duration}/60,60))=60,ROUNDUP({Duration}/3600,0)&":"& “00”,ROUNDDOWN({Duration}/3600,0)&":"& ROUND(MOD({Duration}/60,60))))

Welcome to the community, @Zack_McCarty! :smiley: I tried your formula as well as the one above, and they output exactly the same thing. Here’s the comparison, with yours in the middle, and the one from @Artem_Bourov on the right.

Screen Shot 2020-10-02 at 8.26.17 AM

On the left is one that I wrote a couple months ago (after completely forgetting about this thread), which uses the following formula (my duration field is named “Time”):

IF(
    Time,
    FLOOR(Time / 3600) & ":" & REPT("0", 2 - LEN((MOD(Time, 3600) / 60) & "")) & (MOD(Time, 3600) / 60)
)

They’re all identical, so I’m not sure what’s cleaner about yours, unless the difference is only evident with specific values.

Hi @Justin_Barrett!

when I used the formula at the top, the Mod formulas were showing many decimals, so I simply added the round formulas to ensure that there were no extra decimals showing.

Apologies if that came across negatively! Maybe this was something specific to my machine.

Thanks for the clarification. Duration fields can store fractions of seconds internally, and that might show up in the final formatted version depending on the source of your duration data. However, the duration field’s formatting defaults to rounding the display to whole values. For me (and I’m guessing for @Artem_Bourov as well), the duration will never be fractional, so we never hit that issue when building the final string version, and the shorter formulas are sufficient. It sounds like your duration data often contains fractional values, so that longer formula makes more sense. My gut says that there’s probably a way to make it more compact, but I don’t have time at the moment to look for possible ways to optimize it.

Thanks for the formulas! I needed to convert a duration to a 12 hour AM/PM format with seconds added. Example: duration of zero produces “12:00:00 AM”. Here’s my formula based on the earlier topics in this conversation:

IF(Time,

IF(Time <3600,"12",IF(ROUND(Time / 3600)>12, ROUND(Time / 3600)-12, ROUND(Time / 3600)))

& ":" & REPT("0", 2 - LEN(ROUND(MOD(Time, 3600) / 60) & "")) & ROUND(MOD(Time, 3600) / 60)

& ":" & REPT("0", 2 - LEN(ROUND(MOD(Time, 60)) & "")) & ROUND(MOD(Time, 60))

& IF(ROUND(Time / 3600)>12," PM"," AM")

)

@Airtable_Clerk Thanks for sharing! That formula could be simplified quite a bit:

DATETIME_FORMAT(
    DATEADD(
        DATEADD(
            TODAY(),
            ROUNDDOWN(Time / 3600, 0),
            "hours"
        ),
        MOD(Time, 3600) / 60,
        "minutes"
    ),
    "hh:mm:ss a"
)

Screen Shot 2021-12-25 at 8.18.03 PM

This works by taking advantage of the fact that the time portion of TODAY() is always going to be midnight. Add the hours to that time, then add the minutes, then format it.

2 Likes

Way more simple. I need to handle negative durations and seconds, but I trashed my old code. Thanks!

In what sense? Could you give an example?

This is a Broadcast Automation application where the problem domain is down to the second. I’m doing time calculations where some of the duration fields represent the time (duration since midnight) and some are actual durations. Sometimes actual durations are negative.to schedules things before a certain time.

@Airtable_Clerk Duration fields support negative values, and you can use those in formulas do calculate offsets. Will that work for your use case? (My apologies if I’m misunderstanding something. I’m still not sure if your comment above was a request for assistance, or just a comment sharing how you’re using durations.)

It was just a sharing comment. I’m already using durations and calculating with negative offsets. Thanks though!

1 Like

I have a duration field that is using the DATETIME_DIFF() function. These are for calls so I’d need to know the duration in hh:mm:ss. I know there is the built in formatting feature to change to a duration, but this duration field is being exported into a different program.

If I wanted to change the created hh:mm:ss duration into a string, how would I do that?

@Devan_Kreitzer The formulas above will only handle hours and minutes. Here’s one that handles seconds (change all references to “Time” to point to your duration field):

IF(
    Time,
    FLOOR(Time / 3600) & ":" &
    RIGHT("0" & ROUNDDOWN(MOD(Time, 3600) / 60, 0), 2) & ":" &
    RIGHT("0" & ROUND(((MOD(Time, 3600) / 60) - ROUNDDOWN(MOD(Time, 3600) / 60, 0)) * 60, 0), 2)
)
1 Like

How do need to adjust the formula to avoid having strings like 0:00:14 or 0:05:34

I would need them like this:
1:59
19:59
1:59:59

@Georg_Kuklick This modification will work:

IF(
    Time,
    IF(FLOOR(Time / 3600), FLOOR(Time / 3600) & ":") &
    RIGHT("0" & ROUNDDOWN(MOD(Time, 3600) / 60, 0), IF(NOT(FLOOR(Time / 3600)), MIN(LEN(ROUNDDOWN(MOD(Time, 3600) / 60, 0) & ""), 2), 2)) & ":" &
    RIGHT("0" & ROUND(((MOD(Time, 3600) / 60) - ROUNDDOWN(MOD(Time, 3600) / 60, 0)) * 60, 0), 2)
)

Screen Shot 2022-02-10 at 3.55.03 PM

2 Likes