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

want ask new topic for Code Function,

why im getting blank json after execute function.the data i want to use form google sheet.

here the screenshort

and here the code function

hope can help for this.

Thanks

Hi @mzayn, I don’t have access to your Google Sheet so won’t be able to run your workflow. It also seems you are using a rather old version of the Google Sheets node, and I am not super familiar with the data structure it would use.

If you’re using the latest version of n8n and the Google Sheets node, you should get a data structure like this from the node:

[
  {
    "row_number": 2,
    "ID": 1,
    "First Name": "SpongeBob",
    "Last Name": "SquarePants"
  },
  {
    "row_number": 3,
    "ID": 2,
    "First Name": "Patrick",
    "Last Name": "Star"
  },
  {
    "row_number": 4,
    "ID": 3,
    "First Name": "Squidward",
    "Last Name": "Tentacles"
  },
  {
    "row_number": 5,
    "ID": 4,
    "First Name": "Eugene H.",
    "Last Name": "Krabs"
  },
  {
    "row_number": 6,
    "ID": 5,
    "First Name": "Sheldon J.",
    "Last Name": "Plankton"
  },
  {
    "row_number": 7,
    "ID": 6,
    "First Name": "Karen",
    "Last Name": "Plankton"
  },
  {
    "row_number": 8,
    "ID": 7,
    "First Name": "Sandra \"Sandy\"",
    "Last Name": "Cheeks"
  },
  {
    "row_number": 9,
    "ID": 8,
    "First Name": "Mrs. ",
    "Last Name": "Puff"
  },
  {
    "row_number": 10,
    "ID": 9,
    "First Name": "Pearl",
    "Last Name": "Krabs"
  },
  {
    "row_number": 11,
    "ID": 10,
    "First Name": "Gary",
    "Last Name": "the Snail"
  }
]

So if you look at the first item, that’d be this one:

{
  "row_number": 2,
  "ID": 1,
  "First Name": "SpongeBob",
  "Last Name": "SquarePants"
}

So this wouldn’t be an array and your check Array.isArray(rows) wouldn’t return true. So perhaps you simply want to use code like below instead:

const today = new Date().toISOString().slice(0, 10);

let results = [];

for (const item of $input.all()) {
  const dateExpired = item.json['Date Expired'];
  if (today === dateExpired) {
    results.push({
      json: {
        username: item.json['username'],
        Number_Whatsapp: item.json['Number'],
        ExpiredDate: item.json['Date Expired'],
        Subscribe_Email: item.json['Subscribe - Email'],
        Subscribe_Status_IV_ID: item.json['Subscribe - Status & IV ID'],
        Subscribe_Activation_Date: item.json['Subscribe - Activation Date'],
        Subscribe_Expired_Date: item.json['Subscribe - Expired Date']
      }
    }) 
  }
}

return results;

Going forward it’d be great if you could open a new thread instead of posting on those marked as solved as I tend to overlook these. Thank you :slight_smile:

@MutedJam Thanks.

i see, have an new google sheet awesome. i still use old school google sheet format :rofl:

for new google sheet are better than old one ? and where i can get the reference to use new version of google sheet.
hope old version google sheet can running cause i have a lot of google sheet in other workflow. and take a lot time want to update every new update google sheet.

–

btw, thank for your explanation and fix the function code :+1:

Thanks

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.