I'm trying to calculate the difference between two dates (the early date is June 11th, 1847) as a the number of years plus the number of days. If the second date were June 9th of 2021, I'd like to get back 173 years and 363 days.
I've been using this, though it's not quite what I want.
my $firstdate = DateTime::Format::Strptime->new(pattern => '%Y%m%d')->parse_datetime($papers{$paper}->{'firstday'});
my $today = DateTime::Format::Strptime->new(pattern => '%Y%m%d')->parse_datetime($date);
my $diff = $today - $firstdate;
printf("%d years and %d days\n", $diff->in_units(qw( years days )));
The output looks something like this:
173 years and 29 days
Of course, it's obvious why this is the case if I put months back into the in_units() method... it's because it's calculating those separately, and with the months included it'd give output of 11 months. Plus 29 days, and that's the 363 I want.
Given the regrettable fact of the occasional leap year, this isn't so simple as using the modulus operator.
Is there an elegant way to do this, or barring that, is there a practical way to do this?