String to datetime, calculate time difference (in minutes)

Describe the issue/error/question

I’m trying to solve the following two distinct problems:

  1. I’m trying to convert a string like 15:00 to datetime that I can then consequently work with in n8n
  2. Take a snapshot of the current time and then find out how much time is missing to the date defined in 1)

I’m thinking that the datetime node and luxon will solve most of my problems, but I’m not entirely sure how to go about it. More specifically I’m not sure what the most effective way is to get from the string 15:00, which I will get as a string from an earlier node, to a format that n8n will understand.

  1. String to datetime

All my approaches here have failed me so far.

  1. Calculate how many minutes are left to T

The easiest approach might be to use the datetime node, add {{$now}} and then subtract the time retrieved from 1).

Am I on the right track? Thanks!

Information on your n8n setup

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

Hey @somesimon,

Is the string just 15:00 or is there more to it? It looks like Luxon has a diff() option which can return the difference between 2 datetimes which could be what you are after.

Dear @Jon, thanks for answering!

Well, it’s HH:MM in string which I need to get into the proper format I’m thinking, to then do Luxon’s diff?

I’ve been trying to figure out a way, like using the calculate a time and splitting the string to HH and MM and then add that to the today timestamp (if that makes sense), but is there a better way?


Yes, ok, so I think I finally found what I need, {{DateTime.fromFormat(“23-06-2019”, “dd-MM-yyyy”)}}; I’ll see if I get there.

Edit: ended at this in a function node, I think it does what I need it to do. No idea if it’s the optimal solution, but I’m happy with it!

// Code here will run once per input item.

// game starts at end, how much time do we have till then 'start'
var end = DateTime.fromFormat("11:30", "HH:mm");
var start = $now;

//Calculate minutes left to the game
var diffInMinutes = end.diff(start, 'minutes');

// Add a new field called 'minutesToGame' to the JSON of the item
item.minutesToGame = diffInMinutes.toObject();

// You can write logs to the browser console

//return item;
return item;

If it works, it’s a good solution I’d say :smiley:

Thanks so much for sharing!

1 Like

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