Katya Pas
Katya Pas

Reputation: 33

how to convert string into timestamp in neo4j

I have loaded timestamps in string format using a CSV into Neo4j as I didnt know how to set them to date time format from the beginning. I then found a way to use "split" function on strings to then convert it to integers and then datetime and i got most of it working apart from small issues

I have tried splitting the date bit by "-" and time by ":", but I am not sure how to deal with a space between date and time that makes my soluition not working well

MATCH (p:event)
WITH p, [x IN split(p.tstamp, "-") | toInteger(x)] AS parts
WITH p,parts,   [y IN split(p.tstamp, ":") | toInteger(y)] AS timeparts

SET p.timestamp = datetime({day: parts[2], month: parts[1], year: parts[0], hour: timeparts[0], minute:timeparts [1], second:timeparts[2] 
});

The original timestamp example is:

//original timestamp
09/01/2019 17:34:58.0000

I expected to have e.g:

//e.timestamp           
09/01/2019 17:34:58 

But I got:

//e.timestamp            
"2019-01-01T00:34:58Z"

Upvotes: 1

Views: 2222

Answers (1)

Dima Patserkovskyi
Dima Patserkovskyi

Reputation: 733

You can try to use apoc functions to parse date and time in any format.

I've added event with your original timestamp

{
  "tstamp": "09/01/2019 17:34:58"
}

And query did the work

MATCH (p:Event)
SET p.timestamp = datetime({ epochMillis: apoc.date.parse(p.tstamp, 'ms', 'MM/dd/yyyy HH:mm:ss') })

After this query my event is following:

{
  "tstamp": "09/01/2019 17:34:58",
  "timestamp": "2019-09-01T17:34:58Z"
}

Upvotes: 3

Related Questions