Search code examples
neo4jcypherdate-conversion

Neo4j- convert Time String into Integer in query


How can I convert String Time into Integer in Neo4j. I have been trying for quite a long time, but there does not seem to be any clear solution for it. Only solution, I found is to add a new property to my node while loading it from CSV. I don't want to do this.

I have Time in the following format:

"18:11:00"

and I want to do some subtraction on them.

I tried doing the following but to no avail:

match (st1:Stoptime)-[r:PRECEDES]->(st2:Stoptime)  
return st1.arrival_time, toInt(st1.arrival_time)
limit 10

But I get following output:

"18:11:00"  null

Solution

  • You can install APOC procedures and do it using the function apoc.date.parse:

    return apoc.date.parse('18:11:00','s','HH:mm:ss')
    

    Running this example the output will be:

    ╒════════════════════════════════════════════╕
    │"apoc.date.parse("18:11:00",'s','HH:mm:ss')"│
    ╞════════════════════════════════════════════╡
    │65460                                       │
    └────────────────────────────────────────────┘
    

    The first parameter is the date/time to be parsed. The second parameter is the target time unit. In this case I have specified seconds (s). The third parameter indicates the date/time format of the first parameter.

    Note: remember to install APOC procedures according the version of Neo4j you are using. Take a look in the version compatibility matrix.