Reputation: 882
I am working with a neo4j database and need to be able to have a query check if a date property in a node is before or after the current date. Dates are passed into the database using csv files with dates in this format: mm/dd/yyyy.
Having researched the concept, I found that neo4j does not currently support dates in its release version. How could I get neo4j to do a query where it checks if currentDate > dateProperty (meaning the current date is after the date in the date propert) if neo4j doesn't support dates?
I would prefer not to change the format, unless there was a way to store the date in a format that neo4j can use but display it as mm/dd/yyyy. If not, is there a way I can convert the date to a format that neo4j can use using code within the query?
Upvotes: 2
Views: 10090
Reputation: 316
In the latest version of Neo4j you have both date() and datetime() formats:
https://neo4j.com/docs/cypher-manual/current/functions/temporal/date/ https://neo4j.com/docs/cypher-manual/current/functions/temporal/datetime/
When you set your node property, use syntax similar to this:
Match(n) where id(n) = 123 set n.somedatevalue = datetime("2020-06-08T13:08:00Z")
(There are more variations of date/datetime that can be used, refer to the doc).
You can then do (proper) comparisons of your date/datetime properties when selecting nodes.
Upvotes: 0
Reputation: 67019
[UPDATED with info on APOC in neo4j 3.x]
This query should return all nodes that satisfy currentDate > d.date
. It assumes that you pass a currentDate
parameter in the same mm/dd/yyyy
format.
WITH split({currentDate}, '/') AS cd
MATCH (d:Data)
WITH cd, split(d.date, '/') AS dd, d
WHERE
(cd[2] > dd[2]) OR
(cd[2] = dd[2] AND
((cd[0] > dd[0]) OR
(cd[0] = dd[0] AND (cd[1] > dd[1]))))
RETURN d
NOTE 1: I use the WITH
at the beginning so that the calculation of cd
is only done once. If that logic had been incorporated into the other WITH
clause (which is perfectly legal), then the cd
collection would probably be unnecessarily recalculated for every Data
node.
NOTE 2: This query does not need to convert each date component to an int before comparison as long as every date's format is always exactly mm/dd/yyyy
. For example, mm
must always be 2 numeric characters ('12' > '09' is true
), or the query will not work as is ('12' > '9' is false
).
In neo4j 3.x, APOC procedure support was added (but it has to be installed on the neo4j server), including procedures for date/time support.
Upvotes: 1