Stream: implementers
Topic: FHIRPath date math
Tadas Antanavicius (Dec 10 2019 at 18:56):
Hi,
I want to write a FHIRPath expression that calculates a person's age based on their birthdate.
Doing this elegantly seems like it would require extracting the "year" from both today() and the birthday, e.g pseudocode:
(today.year - birth.year).select($this - iif(today < birth + $this, 1, 0)
Is there an elegant way to extract the year from a Date in FHIRPath?
It seems doable with regex:
today().toString().replaceMatches('(?<year>\d{4}).*', '${year}').toDecimal()
But I'm hoping there's some cleaner way I'm missing. Haven't been able to find anything relevant in these docs: http://build.fhir.org/ig/HL7/FHIRPath/branches/master/N1/
Thanks!
Grahame Grieve (Dec 10 2019 at 20:03):
@Bryn Rhodes there's not even a way to subtract a data from date to get a duration?
Bryn Rhodes (Dec 10 2019 at 20:09):
No, because you'd have to define subtractions for each precision.
Bryn Rhodes (Dec 10 2019 at 20:10):
You can convert both to seconds and get the difference in seconds.
Bryn Rhodes (Dec 10 2019 at 20:10):
But we explicitly put calendar duration calculation out of scope.
Grahame Grieve (Dec 10 2019 at 20:32):
so how would you solve this one?
Bryn Rhodes (Dec 11 2019 at 05:24):
Well to do it right we'd have to introduce date/time component extractors or calendar duration calculation (probably both). It's been on the list, but we have been focused on getting to normative. A perhaps simpler hack would be today().toString().substring(0, 4).toInteger().
Tadas Antanavicius (Dec 11 2019 at 20:14):
Got it, thanks! Going to go with your suggestion for now.
Last updated: Apr 12 2022 at 19:14 UTC