import {Person} from '@app/titles/models/person'; import {FormattedDateTimeRange} from '@common/i18n/formatted-date-time-range'; import {Fragment, memo} from 'react'; interface Props { person: Person; showRange?: boolean; } export const PersonAge = memo(({person, showRange}: Props) => { if (showRange && person.birth_date && person.death_date) { return ( ); } if (person.birth_date) { return ( {calculateAgeFromBirthDate(person.birth_date, person.death_date)} ); } return null; }); function calculateAgeFromBirthDate( _birthDate: string, _deathDate?: string ): number { const until = _deathDate ? new Date(_deathDate) : new Date(); const birthDate = new Date(_birthDate); let age = until.getFullYear() - birthDate.getFullYear(); const m = until.getMonth() - birthDate.getMonth(); if (m < 0 || (m === 0 && until.getDate() < birthDate.getDate())) { age--; } return age; }