Tipp: How to convert time/duration HH:mm into decimal numbers

Hello guys,

after playing around I figured out, that there is no basic conversion option to convert duration time from format HH:mm into decimal format like 4,75 hours.

It’s maybe helpful for some people, how it could be solved (I guess it’s only one way from many)

My solution and steps to test it by yourself:

  1. create a basic trigger module with one value for duration time, e.g. 04:45 (HH:mm)
  2. create a module set variable and use the follwoing function:
    {{sum(parseNumber(substring(1.time; 0; 3)); parseNumber(substring(1.time; 3; 5)) / 60)}}

explanation:

  1. Parsing of first two positions (0;3) → result: 04
  2. Parse the value from string to number
  3. Parsing of last two positions (3;5) → result: 45
  4. parse the value from string to number
  5. Divide Minutes by 60 → 45/60 → result: 0,75
  6. Sum both values to one value

Of course it can be extend by seconds as well if necessary.

That’s it.

I’m also interested in other solutions, if someone has a different approach with functions.

Have fun.

Best regards

PS: I also tried it with a combination of get and split function.

{{parseNumber(get(split(1.time; “:”); 1)) + get(split(1.time; “:”); 2) / 60}}

Hi @Ayko and welcome back to the Make Community!

Have you tried combinations of parseDate and formatDate?

At the bottom of this page, there is a formulat to convert seconds into minutes and hours. A similar approach coult be used for your purposes:

https://www.make.com/en/help/functions/date---time-functions

Off the top of my head, something like:

parseNumber(formatDate(timeString ; “HH”)).round((parseNumber(formatDate(timeString,“mm”))/60*100)

or something like that. I haven’t tested it. But basically, extract the hour, add a period, extract the minutes, divide by 60 then multiply by 100 nd round it off to make sure you don’t get 1.66.67 for 01:40.

Hoe that helps!

L

2 Likes

Added a module export code for your convenience:-

Module Export - quick import into your scenario

You can copy and paste this module export into your scenario. This will import the modules (with fields/settings/filters) described above.

  1. Move your mouse over the line of code below. Copy the JSON by clicking the copy button on the right of the code, which looks like this:

  2. Enter your scenario editor. Press ESC to close any dialogs. Press CTRLV (paste keyboard shortcut for Windows) to paste directly in the editor.

  3. Click on each imported module and re-save it for validation. There may be some errors prompting you to remap some variables and connections.

JSON module export — paste this directly in your scenario

{"subflows":[{"flow":[{"id":1,"module":"util:SetVariable2","version":1,"parameters":{},"mapper":{"name":"duration","scope":"roundtrip","value":"04:45"},"metadata":{"designer":{"x":377,"y":948}}},{"id":2,"module":"util:SetVariable2","version":1,"parameters":{},"mapper":{"name":"duration_hours","scope":"roundtrip","value":"{{trunc(sum(parseNumber(first(split(1.duration; \":\"))); parseNumber(last(split(1.duration; \":\"))) / 60); 2)}}"},"metadata":{"designer":{"x":677,"y":948,"name":"HH:mm to Duration in Hours"}}}]}],"metadata":{"version":1}}

Note: Did you know you can reduce the size of blueprints and module export code like the above, using the Make Blueprint Scrubber?

Hope this helps! Let me know if there are any further questions or issues.

@samliew

P.S.: Investing some effort into the Make Academy will save you lots of time and frustration using Make.

Hey,

thanks for your feedback and inspiration.

I tried it with parseDate and formatDate.

What I figured out:

  • parseDate is working well for Hours
  • paseDate doesn’t work for Minutes

see blueprint.

Best
Ayko

Duration-to-decimal-blueprint.json (7.1 KB)

1 Like

Cool! Don’t forget to mark it as a solution.

L

1 Like