在neo4j

时间:2018-07-25 21:06:46

标签: neo4j cypher

我将 localdatetime()作为时间戳存储在帖子节点中,如果发布时间超过24小时,我不想显示该帖子。所以我在下面写了查询

MATCH (n:Post) return duration.between(localdatetime() ,n.timestamp+duration({hours:24})) as expiry

我正在收到类似这样的回复。

  

P0M1DT-15207.548000000S

     

P0M1DT-4721.134000000S

但是我想在客户端(javascript)中以hh:mm:ss格式显示剩余时间。那么如何将该时间格式格式化为特定格式?

2 个答案:

答案 0 :(得分:2)

持续时间和日期时间功能从3.4.4开始仍在使用,缺少简单的格式化选项。您可以通过accessing the components of the durations拼凑出所需内容:

MATCH (n:Post) 
WITH duration.inSeconds(localdatetime() ,n.timestamp+duration({hours:24})) as expiry
RETURN expiry.hours,  + ':' + expiry.minutesOfHour + ':' + expiry.secondsOfMinute as expiry

如果您需要更一致的格式设置选项,包括将每个单独的字段填充为两位数的能力,则需要执行APOC程序,尤其是apoc.text.format()

MATCH (n:Post) 
WITH duration.inSeconds(localdatetime() ,n.timestamp+duration({hours:24})) as expiry
RETURN apoc.text.format('%02d:%02d:%02d', [expiry.hours, expiry.minutesOfHour, expiry.secondsOfMinute]) as expiry

关于到期时间的默认值为00:00:00,您可以使用CASE语句来表示截止日期的值,然后才能获得从现在到现在的持续时间:

WITH localDateTime() as now
MATCH (n:Post) 
WITH now, n.timestamp + duration({hours:24}) as deadline
WITH now, CASE WHEN now > deadline THEN now ELSE deadline END as deadline
WITH duration.inSeconds(now, deadline) as expiry
RETURN apoc.text.format('%02d:%02d:%02d', [expiry.hours, expiry.minutesOfHour, expiry.secondsOfMinute]) as expiry

答案 1 :(得分:1)

要获取两个日期时间(ab)之间的整天数(作为整数),请使用以下方法:

duration.inDays(a, b).days

例如(假设n.timestamp是neo4j DateTime对象):

MATCH (n:Post)
RETURN duration.inDays(localdatetime(), n.timestamp).days as nWholeDays;

和:

MATCH (n:Post)
RETURN duration.inDays(localdatetime(), n.timestamp).days > 0 AS atLeast24HoursOld;