rluiten / elm-date-extra

Elm Date Extra library add/subtract/diff/format etc dates
http://package.elm-lang.org/packages/rluiten/elm-date-extra/latest
BSD 3-Clause "New" or "Revised" License
75 stars 36 forks source link

Year? #15

Closed toastal closed 8 years ago

toastal commented 8 years ago

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...

rluiten commented 8 years ago

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 ?

toastal commented 8 years ago

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.

rluiten commented 8 years ago

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