Is it possible to manipulate dates in Neo4j? For eg if today's date is 1/1/2017 can I subtract 5 days from the current date and get 12/28/2016
Asked
Active
Viewed 1,283 times
2
-
change your system date ;) – Govind Singh Jan 17 '17 at 13:44
-
1The correct answer is actually `12/27/2016`. – cybersam Jan 17 '17 at 21:42
1 Answers
3
You can use the APOC date/time support functions.
For example:
WITH apoc.date.parse('1/1/2017', 's', 'MM/dd/yyyy') AS startTime
RETURN apoc.date.format(startTime - 5*(60*60*24), 's', 'MM/dd/yyyy');
The above query returns:
12/27/2016
[UPDATED]
Thanks to a great comment from @InverseFalcon, here is an even more succinct and probably more reliable approach. It parses and formats the dates using a resolution of days (instead of seconds, as above), so there is no need to convert from seconds into days (which is generally error-prone as well, due to things such as leap years and day light savings time adjustments).
WITH apoc.date.parse('1/1/2017', 'd', 'MM/dd/yyyy') AS startDate
RETURN apoc.date.format(startDate - 5, 'd', 'MM/dd/yyyy');

cybersam
- 63,203
- 6
- 53
- 76
-
2I just realized APOC supports various resolutions for the time (ms,s,m,h,d), so you should be able to perform this at the day resolution for both parse and format and simply subtract 5. – InverseFalcon Jan 17 '17 at 21:48