cancel
Showing results forΒ
Search instead forΒ
Did you mean:Β

## Calculate duration in seconds and trans to converted to an integer

Node Clone

Hi, all.
I am trying to calculate the duration between two connected nodes and place it on the relationship
This duration is calculated in seconds and is converted to an integer.

E.g..
node:

ID eventtime
1 2020/2/10 10:00:00
2 2020/2/10 10:00:12
3 2020/2/10 11:00:12

Relation:

from_ID to_ID duration
1 2 12
2 3 3600

Thanks.

1 ACCEPTED SOLUTION
Graph Maven

Yes you can do.Suppose you have two nodes with node1. eventime = date1 and node2.eventtime = date2

WITH apoc.date.parse(node1.eventtime,'s','yyyy/MM/dd HH:mm:ss') AS initialTime,
apoc.date.parse(node2.eventtime,'s','yyyy/MM/dd HH:mm:ss') AS finalTime
RETURN finalTime - initialTime as difference.

5 REPLIES 5
Graph Steward

Relationship is already there between the nodes?
Change the eventtime into Datetime and apply duration.inseconds(eventtime1,eventtime2).seconds

Node Clone

Hi,
Relationship is already there between the node.

Is there any way to convert the format on Neo4j?
From '2020/02/10 10:00:00' to '2020-02-10T10:00:00Z'
Thanks for your reply.

Graph Maven
``````Here is a solution.

WITH apoc.date.parse('2020/2/10 10:00:00','s','yyyy/MM/dd HH:mm:ss') AS initialTime,
apoc.date.parse('2020/2/10 10:00:12','s','yyyy/MM/dd HH:mm:ss') AS finalTime
RETURN finalTime - initialTime as difference

result: 12 seconds

WITH apoc.date.parse('2020/2/10 10:00:12','s','yyyy/MM/dd HH:mm:ss') AS initialTime,
apoc.date.parse('2020/2/10 11:00:12','s','yyyy/MM/dd HH:mm:ss') AS finalTime
RETURN finalTime - initialTime as difference

result: 3600 seconds

``````
Node Clone

Hi,
Is there a way to handle all the data?
No specific time required like '2020/2/10 10:00:00'?

Thanks for your reply.

Graph Maven

Yes you can do.Suppose you have two nodes with node1. eventime = date1 and node2.eventtime = date2

WITH apoc.date.parse(node1.eventtime,'s','yyyy/MM/dd HH:mm:ss') AS initialTime,
apoc.date.parse(node2.eventtime,'s','yyyy/MM/dd HH:mm:ss') AS finalTime
RETURN finalTime - initialTime as difference.

Nodes 2022

NODES 2022, Neo4j Online Education Summit

All the sessions of the conference are now available online

Neo4j Resources