日期、时间戳和持续时间
创建和更新值
我们首先创建一些具有 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 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
标题 | 创建时间 | 发布日期 | 阅读时间 |
---|---|---|---|
"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" |