rluiten/elm-date-extra

Year?

toastal opened this issue · 3 comments

Core date offers years as an option for neither Period nor DeltaRecord and I'm touching year differences in some of my own code -- currently there's no convenience for it. 52 weeks and 365 days aren't very exact values to divide against...

Period is limited to doing math that is not affected by input date and time.
Duration lets you use Years in math, but it has no equivalent to DeltaRecord because its not a fixed elapsed time equivalent.

Can you give me an example or two of what you are doing maybe there is another approach ?

In my example I'm given a birth date in the form of a string. In order to turn that into an "age", I run a Date.fromString to get a Maybe Date as well as pipe in the Date.now task. I then ran Maybe.map (Date.Extra.Period.diff now) birthDate to get a Maybe DeltaRecord.

I would prefer to have a year field so I could Maybe.map .year for a Maybe Int to easily pluck off a year. Currently to get a Maybe Int, what I'm doing is floor (((toFloat week) * 7 + (toFloat day)) / 365.25) which is a close approximation, but isn't exactly pretty.

Ok for what your doing it really would suite DeltaRecord for Duration so I spent this evening after work adding diff DeltaRecord to Duration. I think this may suite your case.

It does behave a bit different to Period, though I expect this might suit you as well.
Mentioned in the documentation for Duration.diff but repeated here.

If date1 > date2 then all fields in DeltaRecord will be positive or zero.
If date1 < date2 then all fields in DeltaRecord will be negative or zero.

It was published as 7.0.0