This should work- addresses the two issues i commented on 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 in to 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