The difference between the dates in years

Jonathan M Davis newsgroup.d at jmdavisprog.com
Sat Feb 10 23:48:56 UTC 2024


On Saturday, February 10, 2024 4:20:33 PM MST Steven Schveighoffer via 
Digitalmars-d-learn wrote:
> On Saturday, 10 February 2024 at 15:53:09 UTC, Alexander Zhirov
>
> wrote:
> > Is it possible to calculate the difference between dates in
> > years using regular means? Something like that
> >
> >
> > ```
> > writeln(Date(1999, 3, 1).diffMonths(Date(1999, 1, 1)));
> > ```
> >
> > At the same time, keep in mind that the month and day matter,
> > because the difference between the year, taking into account
> > the month that has not come, will be less.
> >
> > My abilities are not yet enough to figure it out more elegantly.
>
> Maybe I'm not understanding the question, but why not that result
> / 12?

If I understand correctly, he cares about how far into the month the dates
are, whereas diffMonths ignores the smaller units, meaning that you get the
same result no matter when in the month the dates are. So,
2000-05-10 - 1990-05-09 would give 10, whereas 2000-05-10 - 1990-05-30
would give 9. diffMonths / 12 would give 10 in both cases.

- Jonathan M Davis





More information about the Digitalmars-d-learn mailing list