FHIR Chat · FHIRPath date math · implementers

Stream: implementers

Topic: FHIRPath date math


view this post on Zulip 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!

view this post on Zulip 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?

view this post on Zulip Bryn Rhodes (Dec 10 2019 at 20:09):

No, because you'd have to define subtractions for each precision.

view this post on Zulip Bryn Rhodes (Dec 10 2019 at 20:10):

You can convert both to seconds and get the difference in seconds.

view this post on Zulip Bryn Rhodes (Dec 10 2019 at 20:10):

But we explicitly put calendar duration calculation out of scope.

view this post on Zulip Grahame Grieve (Dec 10 2019 at 20:32):

so how would you solve this one?

view this post on Zulip 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().

view this post on Zulip 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