Home > Software engineering >  how can i get age from date of birth in neo4j
how can i get age from date of birth in neo4j

Time:10-25

I´m starting with neo4j and would like to know how to get the age in years. I have the node below

(p1:Person{id:1,birthdate:"1989-10-03",name:"Person1"})

tried something like this

with p.birthdate as bd, date() as d return d - bd

CodePudding user response:

You can use date and duration types, for instance like this:

MATCH (p:Person {name: "Person1"})
WITH duration.between(date(p.birthdate), date()) as d
RETURN d, d.years, d.months, d.days

CodePudding user response:

If you like APOC functions (Awesome Procedures on Cypher) then below script will work:

MATCH (p:Person {name: "Person1"})
RETURN ( apoc.date.convert(timestamp(), 'ms', 'd') - 
         apoc.date.parse(p.birthdate, 'd', 'YYY-MM-DD')) / 365 as yearsAlive 

Result:

╒════════════╕
│"yearsAlive"│
╞════════════╡
│33          │
└────────────┘

Ref:

  • 1.https://neo4j.com/labs/apoc/#:~:text=APOC is an add-on,all APOC functions and procedures

  • 2.https://neo4j.com/labs/apoc/4.0/overview/apoc.date/

  • Related