elm-community / elm-time

A pure Elm date and time library.

Home Page:http://package.elm-lang.org/packages/elm-community/elm-time/latest

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Calculate age in years

muelli opened this issue · comments

I think I am similarly confused to what has been reported in #46.

Let's assume I have two Dates (or DateTimes for that matter). I want to know how many years have past from one point in time to another. Mainly for showing the age in years. It seems impossible to do with the current library.

How would I find out the correct age without resorting to weird hacks like dividing the days passed by 365.25 to filter out the leap years correctly?

> Time.Date.delta (Time.Date.date 2018 4 1) (Time.Date.date 1932 4 2) |> .days |> toFloat |> flip (/) 365.25
85.99589322381931 : Float
> Time.Date.delta (Time.Date.date 2018 4 2) (Time.Date.date 1932 4 2) |> .days |> toFloat |> flip (/) 365.25
85.99863107460644 : Float
> Time.Date.delta (Time.Date.date 2018 4 3) (Time.Date.date 1932 4 2) |> .days |> toFloat |> flip (/) 365.25
86.00136892539356 : Float
> 

Did you try

Time.Date.delta (Time.Date.date 2018 4 1) (Time.Date.date 1932 4 2) |> .years

yes. You, too?

It is very deceiving, but it does not work:

> import Time.Date
> Time.Date.delta (Time.Date.date 2018 4 3) (Time.Date.date 1932 4 2) |> .years
86 : Int
> Time.Date.delta (Time.Date.date 2018 4 1) (Time.Date.date 1932 4 2) |> .years
86 : Int
> 

And the code is quite clear that it is incapable of doing what is required:

{ years = d1.year - d2.year

Ahh, I see what you're pointing out now. Yeah, I'd consider this a bug. Thanks for noticing and submitting the issue. If you have a fix and would like to submit a PR, I'd be glad to process it. I have an idea for the fix and will try it if you don't.

I'm presently the maintainer of this package, and I can say that time processing has changed radically for Elm 0.19 (which was just released). I haven't gone over the details yet, but here's the new Time package. If you are planning on upgrading to Elm 0.19, you might want to check it out.

Got bitten by this today. Actually, it isn't a bug, the current behavior is explicitly outlined in the docs along with a couple of examples. I somehow miss the functionality that date-fns was providing me in JS.

Would you accept PRs that add the missing functionality?

Am archiving this repository which recommends closing this issue