Calculating a date as 18 months after a reference date

I am trying to calculate a date as 18 calendar months after a reference date.

This is what I am currently doing as a stopgap but I am hoping that there is a better way to resolve this as ideally I would love to just add calendar months to the reference date so that the following endDate resolves to 2025-02-21 instead of 2025-02-18.

WITH apoc.date.parse("2023-08-21 17:48", "ms", "yyyy-MM-dd HH:mm") as refDatetime, apoc.date.add(refDatetime, "ms", 547, "days") as endDatetime
return apoc.date.format(refDatetime, "ms", "yyyy-MM-dd") as refDate, apoc.date.format(endDatetime, "ms", "yyy-MM-dd") as endDate

Returns:

refDate endDate
2023-08-21 2025-02-18

Notes:

  • Since the apoc.date.add(...) method doesn't support "adding" months, I'm using days (30.437 average days/month * 18 months = 547.8 ish, and then rounding it down).

DBMS info:

  • Version: 5.19.0
  • Edition: community
  • Plugins: apoc

I realize I can just write some sort of script to calculate these values but I wanted to check in with the community first to see if this is even possible using just cypher.

Thanks in advance!

Try this:

with datetime("2023-08-21T17:48") as datetime
return datetime + Duration({months: 18})

1 Like

And, if you just have a date:

with date("2023-08-21") as date
return date + Duration({months: 18})

For some reason I was convinced I couldn't mix usage of Duration + apoc calls. Thanks so much!

1 Like