我目前正在收到错误
的字符串不能表示值:{年份:{低:2020年,高:0 },月份:{低: 7,高:0 },日:{低: 1,高:0 },小时:{低: 7,高:0 },分钟:{低: 35,高:0 },第二:{低: 45,高:0 },纳秒:{低: 914000000,高:0 },timeZoneOffsetSeconds:{低:0 },timeZoneId: 0 }‘
这是由于我的一个属性被存储为datetime()
造成的。如何将其转换为字符串?
我正在使用npm的新4j驱动程序。
发布于 2021-09-06 16:45:59
这应该是可行的--解决我对接受的答案提出的两个问题。也许op已经解决了这个问题,但希望这能帮助到其他人。
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输出-
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
发布于 2022-11-15 07:15:17
看看neo4j.types.DateTime。
const datetime= new Date()
const dateToNeo = neo4j.types.DateTime.fromStandardDate(datetime);
发布于 2020-07-01 08:50:10
因为DateTime返回了一堆neo4 int样式的东西。
DateTime {
year: Integer { low: 2020, high: 0 },
month: Integer { low: 7, high: 0 },
day: Integer { low: 1, high: 0 },
hour: Integer { low: 8, high: 0 },
minute: Integer { low: 48, high: 0 },
second: Integer { low: 18, high: 0 },
nanosecond: Integer { low: 833000000, high: 0 },
timeZoneOffsetSeconds: Integer { low: 0, high: 0 },
timeZoneId: null
}
我正在用.toString()转换字段
export function convertDateToString (
date)
: string {
const date_values = [];
for (const key in date) {
const cursor = date[key];
if (cursor) {
date_values.push(cursor.toString());
}
}
const the_date = new Date(...date_values as [number, number, number, number, number, number]);
return the_date.toUTCString();
}
https://stackoverflow.com/questions/62671936
复制相似问题