javascript neo4j-driver如何将日期时间转换为字符串

kmynzznz  于 2022-11-23  发布在  Java
关注(0)|答案(3)|浏览(226)

我目前收到错误
'字符串不能表示值:{年份:{低:2020年,高:0 },月份:{低:7,高:0 },日:{低:1,高:0 },小时:{低:7,高:0 },分钟:{低:35,高:0 },秒:{低:45,高:0 },纳秒:{低:914000000,高:0 },时区偏移秒数:{低:0,高:0 },时区ID:空}'
这是由于我的一个属性被存储为datetime()而导致的。如何将其转换为字符串?
我正在使用npm的neo4j驱动程序

aelbi1ox

aelbi1ox1#

这应该会起作用-解决了我对已接受答案评论的两个问题。也许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
wecizke3

wecizke32#

查看neo4j.types.DateTime。

const datetime= new Date()
const dateToNeo = neo4j.types.DateTime.fromStandardDate(datetime);
n53p2ov0

n53p2ov03#

由于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();
}

相关问题