日期、日期时间和持续时间
创建和更新值
让我们从创建一些具有 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 秒。
也许我们想要对这个文章节点进行一些更改,以更新 datePublished
和 readingTime
属性。
我们决定在下周而不是今天发布这篇文章,因此我们想要进行更改。如果我们想要使用 支持的格式 创建一个新的 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
标题 | 创建 | 发布时间 | 阅读时间 |
---|---|---|---|
"日期、日期时间和持续时间在 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
标题 | 创建 | 发布时间 | 阅读时间 |
---|---|---|---|
"日期、日期时间和持续时间在 Neo4j 中" |
"2019 年 9 月 25 日 06:29" |
"2019 年 9 月 29 日" |
"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
标题 | 创建 | 发布时间 | 阅读时间 |
---|---|---|---|
"Cypher 基础 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
如果运行此查询,我们将获得以下结果
标题 | 创建 | 发布时间 | 阅读时间 |
---|---|---|---|
"Cypher 基础 I" |
2019-06-01T18:40:32.142+01:00 |
2019-06-01 |
P0M0DT135S |
这似乎不对 - 那么在 2019 年 6 月 2 日发布的 Cypher 基础 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
标题 | 创建 | 发布时间 | 阅读时间 |
---|---|---|---|
"Cypher 基础 I" |
2019-06-01T18:40:32.142+01:00 |
2019-06-01 |
P0M0DT135S |
"Cypher 基础 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
标题 | 创建 | 发布时间 | 阅读时间 |
---|---|---|---|
"日期、日期时间和持续时间在 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
标题 | 创建 | 发布时间 | 阅读时间 |
---|---|---|---|
"Cypher 基础 I" |
"2019 年 6 月 1 日 18:40" |
"2019 年 6 月 1 日" |
"02:15" |
"Cypher 基础 II" |
"2019 年 6 月 2 日 10:23" |
"2019 年 6 月 2 日" |
"02:30" |