How to calculate time + convert time to 1. hour and 2. minute?

Describe the issue/error/question

Hi Community.

need help form your.

  • List item
  1. in my situation now - i was add the time via node Data And Time > {{new Date()}} to get UTC time.
    and use operation (ADD) - 5 Minute option

like this sample :

  • and got the result time not in the option format was given.

  1. **How to i convert that result in:
  • Hour Option

  • Minutes Option

1. Hour | 2. Minute ~ option ? any function to use ? let me now. for this.

Like this option i needed :
image

Thanks

What is the error message (if any)?

  • NA

Please share the workflow

(Select the nodes and use the keyboard shortcuts CMD+C/CTRL+C and CMD+V/CTRL+V to copy and paste the workflow respectively)

Share the output returned by the last node

Hope can help how to convert that result to Minute and Hour.

Information on your n8n setup

  • n8n version: : 0.194.0
  • Database you’re using (default: SQLite): : default
  • Running n8n with the execution process [own(default), main]: : default
  • Running n8n via [Docker, npm, n8n.cloud, desktop app]: : docker

Hi @mzayn, welcome to the community!

You might want to have a look at Luxon which is included in n8n. This works through expressions rather than the Date & Time node.

To get the hours and minutes of “now + 5 minutes” you can use an expression such as {{ $now.plus({minutes: 5}).toFormat('hh:mm') }}.

To get hours and minutes as a fraction of the hours you could use some basic maths such as {{ $now.plus({minutes: 5}).hour + ($now.plus({minutes: 5}).minute / 60) }} (essentially summing up the hours and the minute part of the time in 5 minutes, with the latter being divided by 60).

Getting the minutes works in a similar fashion only I’m multiplying the hours with 60 here: {{ $now.plus({minutes: 5}).hour * 60 + $now.plus({minutes: 5}).minute }}.

Results would look like so:

Here’s how it looks in a workflow:

Depending on what you are doing you might also want to take a look at durations in Luxon which might be more suitable for many use cases (since you wouldn’t need to actively ignore the date portion of a date or worry about time zones).

1 Like

hi @MutedJam,

Thanks for the solution.

but for my situation.

  1. first i want to capture date now. what time the date was trigger to send at sheet. > via date and time node. The time are using in UTC and will converted to GMT8+.

  2. second, i want get the capture in (1) to convert that time in (1. hour) and (2. minutes) where for trigger the exam was start and end.

  3. this situation for i make some trigger some exam/class was been start and end. > and now i need some function to capture (hh:mm), (hour), and (minutes) to set in the sheet.

That all.
thanks again for the Solution :+1:

1 Like

Perfect, sounds like the timestamps should do the job then.

If you simply want to split up the hour and minutes you can obviously remove the calculation part and just use $now.plus({minutes: 5}).hour for the hours or $now.plus({minutes: 5}).minute to get the minutes.

2 Likes

Hi @MutedJam

1 more question

how to make some calculate for

{{$now.plus({minutes: 5}).toFormat('hh:mm')}} - {{$now.toFormat('hh:mm') }}

mean want to calculate that node in $now.plus({minutes: 5}).toFormat(‘hh:mm’) minus (-) $now.toFormat(‘hh:mm’)

what function to use - want to get the result in minute ?

Thanks

That would result in a duration and you could do it as described in the n8n or Luxon documentation pages linked above. The basic syntax would be end.diff(start). So in an n8n expression something like {{ $now.plus({minutes: 5}).diff($now) }}:

Keep in mind that formatting will work different for durations than it does for timestamps. Getting the total minutes for example would work by adding .as('minutes') to the expression above (so {{ $now.plus({minutes: 5}).diff($now).as('minutes') }}).

1 Like

@MutedJam thanks for the info.

but right now i still want to get in hour, minutes
like eg result:
1 hour 24 minutes Or 1 days 13 hour 10 minutes

using this code

 // this data date retrieve form another node in sheet via json sample (2022-09-22T16:06:50) in the sheet

{{ DateTime.fromISO($now).diff(DateTime.fromISO($node["sheet1"].json["timer-was-set"]), ['days','hours','minute','second']).toObject() }}

here my node setting

the result i got Error say object : [Object: {}]


hm how to set this in node ?

Thanks for helping :+1: :smile:

Hi @mzayn, can you share the JSON data returned by your sheet1 node? That should make it a lot easier to understand what we’re working with here.

I am asking because your example works fine from me when I provide some mock data like so:

Result:

1 Like

hi @MutedJam

for sheet1 that already solve cause happen have spacing there.


further, how to make that setting hour, minutes like this 1 Hours 30 Minute

still no idea how to change that code.

thanks