日期、日期时间与持续时间

创建和更新值

让我们从创建一个具有日期时间(Datetime)属性的节点开始。我们可以通过执行以下 Cypher® 查询来实现

UNWIND [
    { title: "Cypher Basics I",
      created: datetime("2019-06-01T18:40:32.142+0100"),
      datePublished: date("2019-06-01"),
      readingTime: {minutes: 2, seconds: 15} },
    { title: "Cypher Basics II",
      created: datetime("2019-06-02T10:23:32.122+0100"),
      datePublished: date("2019-06-02"),
      readingTime: {minutes: 2, seconds: 30} },
    { title: "Dates, Datetimes, and Durations in Neo4j",
      created: datetime(),
      datePublished: date(),
      readingTime: {minutes: 3, seconds: 30} }
] AS articleProperties

CREATE (article:Article {title: articleProperties.title})
SET article.created = articleProperties.created,
    article.datePublished = articleProperties.datePublished,
    article.readingTime = duration(articleProperties.readingTime)

在此查询中

  • created 属性是一个 DateTime 类型,等于查询执行时的日期时间。

  • date 属性是一个 Date 类型,等于查询执行时的日期。

  • readingTime 是一个 Duration(持续时间)类型,为 3 分 30 秒。

也许我们想对这个文章节点做一些修改,以更新 datePublishedreadingTime 属性。

我们决定在下周而不是今天发布这篇文章,因此我们想做出改变。如果我们想使用支持的格式来创建新的 Date 类型,我们可以使用以下查询

MATCH (article:Article {title: "Dates, Datetimes, and Durations in Neo4j"})
SET article.datePublished = date("2019-09-30")

但是,如果我们想基于不支持的格式创建 Date 类型该怎么办?为此,我们将使用 APOC 库中的函数来解析字符串。

以下查询将一种不支持的数据格式解析为基于毫秒的时间戳,从该时间戳创建一个 Datetime,然后从该 Datetime 创建一个 Date

WITH apoc.date.parse("Sun, 29 September 2019", "ms", "EEE, dd MMMM yyyy") AS ms
MATCH (article:Article {title: "Dates, Datetimes, and Durations in Neo4j"})
SET article.datePublished = date(datetime({epochmillis: ms}))

我们可以使用相同的方法来更新 created 属性。唯一需要修改的是,我们不需要将 Datetime 类型转换为 Date

WITH apoc.date.parse("25 September 2019 06:29:39", "ms", "dd MMMM yyyy HH:mm:ss") AS ms
MATCH (article:Article {title: "Dates, Datetimes, and Durations in Neo4j"})
SET article.created = datetime({epochmillis: ms})

也许我们还决定阅读时间实际上比我们最初想象的要多一分钟。我们可以通过以下查询更新 readingTime 属性

MATCH (article:Article {title: "Dates, Datetimes, and Durations in Neo4j"})
SET article.readingTime =  article.readingTime + duration({minutes: 1})

格式化值

现在我们要编写一个查询来返回我们的文章。我们可以通过执行以下查询来实现

MATCH (article:Article)
RETURN article.title AS title,
       article.created AS created,
       article.datePublished AS datePublished,
       article.readingTime AS readingTime
表 1. 结果
标题 created datePublished readingTime

"Neo4j 中的日期、日期时间和持续时间"

2019-09-25T06:29:39Z

2019-09-29

P0M0DT270S

如果我们想格式化这些值,可以使用 APOC 库中的时间函数。以下查询将每种时间类型格式化为更友好的格式

MATCH (article:Article)
RETURN article.title AS title,
       apoc.temporal.format(article.created, "dd MMMM yyyy HH:mm") AS created,
       apoc.temporal.format(article.datePublished,"dd MMMM yyyy") AS datePublished,
       apoc.temporal.format(article.readingTime, "mm:ss") AS readingTime
表 2. 结果
标题 created datePublished readingTime

"Neo4j 中的日期、日期时间和持续时间"

"25 September 2019 06:29"

"29 September 2019"

"04:30"

比较和筛选值

如果我们想基于这些时间值来筛选文章该怎么办?

让我们先找到 2019 年 6 月 1 日发布的文章。以下查询实现了这一点

MATCH (article:Article)
WHERE article.datePublished = date({year: 2019, month: 6, day: 1})
RETURN article.title AS title,
       article.created AS created,
       article.datePublished AS datePublished,
       article.readingTime AS readingTime
表 3. 结果
标题 created datePublished readingTime

"Cypher Basics I"

2019-06-01T18:40:32.142+01:00

2019-06-01

P0M0DT135S

如果我们想找到 2019 年 6 月发布的所有文章呢?我们可以编写以下查询来实现

MATCH (article:Article)
WHERE article.datePublished = date({year: 2019, month: 6})
RETURN article.title AS title,
       article.created AS created,
       article.datePublished AS datePublished,
       article.readingTime AS readingTime

如果我们运行此查询,将得到以下结果

表 4. 结果
标题 created datePublished readingTime

"Cypher Basics I"

2019-06-01T18:40:32.142+01:00

2019-06-01

P0M0DT135S

这似乎不对——那篇在 2019 年 6 月 2 日发布的 Cypher Basics II 文章呢?这里的问题在于 date({year: 2019, month:6}) 返回的是 2019-06-01,所以我们只能找到 2019 年 6 月 1 日发布的文章。

我们需要调整查询以查找 2019 年 6 月 1 日至 2019 年 7 月 1 日之间发布的文章。以下查询实现了这一点

MATCH (article:Article)
WHERE date({year: 2019, month: 7}) > article.datePublished >= date({year: 2019, month: 6})
RETURN article.title AS title,
       article.created AS created,
       article.datePublished AS datePublished,
       article.readingTime AS readingTime
表 5. 结果
标题 created datePublished readingTime

"Cypher Basics I"

2019-06-01T18:40:32.142+01:00

2019-06-01

P0M0DT135S

"Cypher Basics II"

2019-06-02T10:23:32.122+01:00

2019-06-02

P0M0DT150S

如果我们想基于存储 Datetime 值的 created 属性进行筛选呢?在筛选 Datetime 值时,我们需要采取与 Date 值相同的方法。以下查询查找 2019 年 7 月之后创建的文章

MATCH (article:Article)
WHERE article.created > datetime({year: 2019, month: 7})
RETURN article.title AS title,
       article.created AS created,
       article.datePublished AS datePublished,
       article.readingTime AS readingTime
表 6. 结果
标题 created datePublished readingTime

"Neo4j 中的日期、日期时间和持续时间"

2019-09-25T06:04:39.072Z

2019-09-25

P0M0DT210S

最后是筛选持续时间。我们可能想找到阅读时间在 3 分钟或更短的文章。

我们将从以下查询开始

MATCH (article:Article)
WHERE article.readingTime <= duration("PT3M")
RETURN article.title AS title,
       article.created AS created,
       article.datePublished AS datePublished,
       article.readingTime AS readingTime

然而,该查询的结果是:无更改,无记录

如果我们想比较持续时间,我们需要通过将这些持续时间加到日期上来进行比较。我们并不真正关心查询中的具体日期,所以我们将使用当前时间来绕过这个问题。我们可以通过调用 datetime() 函数 来获取当前时间。

我们更新后的查询如下所示

MATCH (article:Article)
WHERE datetime() + article.readingTime <= datetime() + duration("PT3M")
RETURN article.title AS title,
       article.created AS created,
       article.datePublished AS datePublished,
       article.readingTime AS readingTime
表 7. 结果
标题 created datePublished readingTime

"Cypher Basics I"

"01 June 2019 18:40"

"01 June 2019"

"02:15"

"Cypher Basics II"

"02 June 2019 10:23"

"02 June 2019"

"02:30"

资源

本节展示了如何使用 APOC 库更有效地处理时间类型。以下是关于在 Neo4j 中使用时间类型的更多学习资源