cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

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().

5 REPLIES 5

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.

https://neo4j.com/docs/cypher-manual/current/functions/temporal/

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()
2019-01-06T05:00:00.000Z

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.

folterj
Node Clone

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

Elaine

Hi guys,

Is there any difference in performance using something like this:

MATCH (n) WHERE n.timestamp >= datetime({year: 2019, month: 1, day: 1}).epochMillis  RETURN n

Versus declaring the datetime in the beginning of the query, as @elaine.rosenberg suggested?

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

Does Neo4j compute the function datetime() n times in the first case and only computes one time in the second case?

I ran a test in a query that has about 1M nodes and the second option was consistently 2 seconds faster (22s vs 20s).