Comparing dates in cypher

What is the BEST practice to compare two dates in CYPHER? I use the cypher timestamp() but I need to compare it to a parameter date in the format: moment().startOf('week').toDate().

Have a look at the manual on temporal functions. Neo4j 3.4.x comes with a lot of date functions built into it. And in terms of straight comparison, you can just use <, >, and so on to compare them.

If there's a particular type of comparison you're trying to do, let us know.

In the case of moment dates, if you're using javascript and you have an ISO formatted date string like:

> moment().startOf('week').toDate()

Then you can feed that into neo4j's datetime function and then use it as an instant and use all of the cypher functions linked above.


Thanks David. I wanted to store date as moment().format to later use it as a sort variable. I have some issues with getting my result sorted on time.....I think I will do it in Javascript.

Comparing dates in cypher does not seem straight-forward. The documentation (link cited above) mainly looks at declaration and conversion, but no comparisons.
i.e. the following and variations do not return any results (with 'timestamp' valid cypher datetime object)

MATCH (n) where n.timestamp >= date({year:2019,month:1}) RETURN n
MATCH (n) where n.timestamp <= datetime({year:2019,month:1}) RETURN n

Any help appreciated.

How you compare n.timestamp will depend upon how that value was set.

Perhaps you could do something like:

WITH datetime({year: 2019, month: 1, day: 1}).epochMillis AS testDate
// or
// WITH datetime({year: 2019, month: 1, day: 1}).epochSeconds AS testDate
MATCH (n) WHERE n.timestamp >= testDate RETURN n


1 Like