cancel
Showing results for 
Search instead for 
Did you mean: 

Calculate duration in seconds and trans to converted to an integer

pawa19961996
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

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.

View solution in original post

5 REPLIES 5

intouch_vivek
Graph Steward

@pawa19961996,

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

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.

ameyasoft
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

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.

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
NODES 2022, Neo4j Online Education Summit

On November 16 and 17 for 24 hours across all timezones, you’ll learn about best practices for beginners and experts alike.