How to convert a number to formatted time duration?

Is there a way to convert a number from a step to a formatted time duration of seconds, minutes, and hours? For example, take the number 121 from a step, and convert it into 0h 2m 1s.

Hi @rkramer,

To convert a number from a step into a formatted time duration of seconds, minutes, and hours in a Pipedream workflow, you can use a Node.js code step. Here’s an example of how you might write that code:

export default defineComponent({
  async run({ steps, $ }) {
    const seconds = steps.previous_step_name.seconds;
    const hours = Math.floor(seconds / 3600);
    const minutes = Math.floor((seconds % 3600) / 60);
    const remainingSeconds = seconds % 60;

    const formattedDuration = `${hours}h ${minutes}m ${remainingSeconds}s`;
    return formattedDuration;
  },
})

In this code, replace steps.previous_step_name.seconds with the actual reference to the step and variable that contains the number you want to convert. This code calculates the hours, minutes, and remaining seconds, then formats them into a string.

Remember to adjust the previous_step_name and seconds to match the name of the step and the variable from your workflow that contains the number you want to convert.
I’m a bot powered by Pipedream and GPT-4. I’m still learning, so please double check my answers and code! Learn how to build your own.

Hi @rkramer,

To convert a number from a step into a formatted time duration of seconds, minutes, and hours in a Pipedream workflow, you can use a Node.js code step. Here’s an example of how you might write this code:

export default defineComponent({
  async run({ steps, $ }) {
    const totalSeconds = steps.previousStepName.seconds;
    const hours = Math.floor(totalSeconds / 3600);
    const minutes = Math.floor((totalSeconds % 3600) / 60);
    const seconds = totalSeconds % 60;

    const formattedDuration = `${hours}h ${minutes}m ${seconds}s`;
    return formattedDuration;
  },
})

In this example, replace steps.previousStepName.seconds with the actual reference to the step and variable that contains the number you want to convert. This code calculates the hours, minutes, and seconds, and then formats them into a string.
I’m a bot powered by Pipedream and GPT-4. I’m still learning, so please double check my answers and code! Learn how to build your own.

1 Like