I'm currently getting the error
'String cannot represent value: { year: { low: 2020, high: 0 }, month: { low: 7, high: 0 }, day: { low: 1, high: 0 }, hour: { low: 7, high: 0 }, minute: { low: 35, high: 0 }, second: { low: 45, high: 0 }, nanosecond: { low: 914000000, high: 0 }, timeZoneOffsetSeconds: { low: 0, high: 0 }, timeZoneId: null }'
which is caused by one of my properties being stored as datetime()
. What would be the way to convert this into a string?
I am using the neo4j-driver from npm
Update 2024-10-01
Version: neo4j-5.0
Neo4j now has the toStandardDate
method in it's returned Date object.
Example Usage:
const { records } = neo4jDriver.executeQuery(`MATCH (n {id: 1}) RETURN n`)
console.log(records[0].get('n').properties.dateTimeProperty.toStandardDate())
Should output:
2020-07-01T07:35:45.914Z
This should work - it addresses the two issues I commented on in the accepted answer. Perhaps op already worked this out, but hopefully, this will help somebody else.
import { DateTime } from 'neo4j-driver';
/**
* Convert neo4j date objects into a parsed javascript date object
* @param dateString - the neo4j date object
* @returns Date
*/
const parseDate = (neo4jDateTime: DateTime): Date => {
const { year, month, day, hour, minute, second, nanosecond } = neo4jDateTime;
const date = new Date(
year.toInt(),
month.toInt() - 1, // neo4j dates start at 1, js dates start at 0
day.toInt(),
hour.toInt(),
minute.toInt(),
second.toInt(),
nanosecond.toInt() / 1000000 // js dates use milliseconds
);
return date;
};
console.log output to compare dates:
DateTime {
year: Integer { low: 2021, high: 0 },
month: Integer { low: 9, high: 0 },
day: Integer { low: 6, high: 0 },
hour: Integer { low: 15, high: 0 },
minute: Integer { low: 41, high: 0 },
second: Integer { low: 30, high: 0 },
nanosecond: Integer { low: 184000000, high: 0 },
timeZoneOffsetSeconds: Integer { low: 0, high: 0 },
timeZoneId: null
}
2021-09-06T15:41:30.184Z