Home > Net >  Neo4j: Converting string to datetime
Neo4j: Converting string to datetime

Time:07-28

I have my data loaded on Neo4j instance where, for each node, I recorded the temporal information in a property key (.time) following the format:

YYYY-MM-DD

example: time: 1937-01-01

These are all strings, that I would like to convert into datetime as to use them in Neo4j Bloom and various time-based queries. I tried to use the following formula (as well as various variations of it):

MATCH (p:Image)
WHERE p.time IS NOT NULL
SET p.time = datetime({ epochMillis: apoc.date.parse(p.time, 's', 'yyyy-MM-dd HH:mm:ss') })

without success. I always get the error

Failed to invoke function `apoc.date.parse`: Caused by: java.text.ParseException: Unparseable date: "1891-01-01"

Any idea what I am doing wrong and how to transform date as string to datetime ?

I read some previous posts on the subject but I couldn't find a satisfying answer...

CodePudding user response:

Your data format and the format you specified in the apoc function don't match. Also, you might wanna parse milliseconds, rather than seconds, since you are using epochMillis field.

Try this:

MATCH (p:Image)
WHERE p.time IS NOT NULL AND p.time <> ""
SET p.time = datetime({ epochMillis: apoc.date.parse(p.time   " 00:00:00", 'ms', 'yyyy-MM-dd HH:mm:ss') })

Ok, then for your error you will have to use your previous format, and convert your date into your format. The error basically meant that millisecond component is not present.

CodePudding user response:

You can use function date() if you have only date part without time. You can call date('2015-07-21') directly.

https://neo4j.com/docs/cypher-manual/current/functions/temporal/#functions-date-create-string

  • Related