Search code examples
neo4jcypher

Neo4J / Cypher - Calculate durations between 2 without weekend days


Is there a way with neo4j / cypher to calculate the difference in days between two dates without including weekends (basically only counting worked/company open days) ?

Something in the line of this query :

return duration.inDays(date("2022-01-01"), date("2022-01-19")).days

But without the weekend days.

How could I achieve this ?


Solution

  • There is no date function for working days but we can compute it by creating a list of dates between start date and end date. Then using the function weekDay where weekday:1 is monday and 5 is friday, we can remove the weekends from the list. Then count the number for working days in that list.

       WITH date("2022-01-01") AS startDate, date("2022-10-19") AS endDate
       WITH startDate, duration.inDays(startDate, endDate).days AS days
       WITH [day in range(0, days) | startDate + duration({days: day})] as calendarDays
       WITH [c in calendarDays where c.weekDay < 6 | c] as workingDays
       RETURN size(workingDays) as workingDays
    
    
    ╒═════════════╕
    │"workingDays"│
    ╞═════════════╡
    │208          │
    └─────────────┘