Import csv format issue: convert str date to datetime format

hi there,

i have a csv file where it contains ID, source node, target node, date, sentiment. i would like to load it into neo4j, but when i load the csv file, it will read my date as string. i tried to change to datetime(row.date) or date(row.date) .
but no luck, giving me error
Text cannot be parsed to a DateTime "7/1/2020"

Here is my sample of csv:
ID,source,target,date,sentiment
1,a,b,7/1/2020,positive
2,a,c,7/1/2020,negative

LOAD CSV WITH HEADERS FROM 'file:///test.csv' AS row
MERGE (source:Character {name: row.source})
MERGE (target:Character {name: row.target})
WITH source, target, row
MERGE (source)-[r:INTERACTS {date: datetime(row.date), sentiment: row.sentiment}]->(target)

how would i have to change my date columns to my csv file so that it can parse a datetime into neo4j ? or how would i able to convert the date columns into datetime in neo4j?

please advise!! thanks!!

The date format you are using is not supported by the "datetime" method. One option is to parse the data string into its components and pass a map to the date method.

Note, I used "date" instead of "datetime" as you did not have a time component. Using datetime with just a date will just add "zero" time UTC to the date.

LOAD CSV WITH HEADERS FROM 'file:///Book1.csv' AS row
WITH row, split(row.date, "/") as date
WITH row, datetime({
    year:toInteger(date[2]), 
    month:toInteger(date[0]),
    day:toInteger(date[1])
}) as convertedDate
MERGE (source:Character {name: row.source})
MERGE (target:Character {name: row.target})
MERGE (source)-[r:INTERACTS {date: convertedDate, sentiment: row.sentiment}]->(target)

Note: you could avoid the extra work if you could change your import file to provide the date in yyyy-mm-dd format instead, as this is one of the supported formats of the “date” procedure.

2 Likes

it works!!! thanks so much :smiley:

1 Like