# Getting current time

**URL:** <https://discourse.elm-lang.org/t/getting-current-time/2434>\
**Category:** Learn\
**Created:** [November 3, 2018, 3:54pm UTC](https://discourse.elm-lang.org/t/getting-current-time/2434 "2018-11-03T15:54:23Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![stevensonmt](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/stevensonmt/32/889_2.png) [@stevensonmt](https://discourse.elm-lang.org/u/stevensonmt)\
**Post date:** [November 3, 2018, 3:54pm UTC](https://discourse.elm-lang.org/t/getting-current-time/2434/1 "2018-11-03T15:54:23Z")

</div>

I’m struggling to understand how to get the current time with Elm. I would expect that `Time.now` would return a `Time` or `Int` or `Float` but instead it is the ever-confusing `Task`. I want to use the current Posix time in milliseconds to generate keys for a `Dict` when creating new entries. I’m hopeless with this concept.

---

<div class="post-metadata">

**Author:** ![malaire](https://avatars.discourse-cdn.com/v4/letter/m/b782af/32.png) [@malaire](https://discourse.elm-lang.org/u/malaire)\
**Post date:** [November 3, 2018, 3:57pm UTC](https://discourse.elm-lang.org/t/getting-current-time/2434/2 "2018-11-03T15:57:40Z")

</div>

There is short example of getting time here in official guide: [https://guide.elm-lang.org/effects/time.html](https://guide.elm-lang.org/effects/time.html)

---

<div class="post-metadata">

**Author:** ![stevensonmt](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/stevensonmt/32/889_2.png) [@stevensonmt](https://discourse.elm-lang.org/u/stevensonmt)\
**Post date:** [November 3, 2018, 4:13pm UTC](https://discourse.elm-lang.org/t/getting-current-time/2434/3 "2018-11-03T16:13:35Z")

</div>

I’ve read through that several times but don’t understand how to translate constantly updating every second for just getting the time when I click a button. My problem is not with the Time module per se, but with Tasks in general. I don’t understand the need for them and what they accomplish. When I say “Elm, I want the current time” I expect Elm to return the current time. Instead Elm returns some Task awaiting some other ingredient that I don’t know how to supply.

---

<div class="post-metadata">

**Author:** ![malaire](https://avatars.discourse-cdn.com/v4/letter/m/b782af/32.png) [@malaire](https://discourse.elm-lang.org/u/malaire)\
**Post date:** [November 3, 2018, 4:36pm UTC](https://discourse.elm-lang.org/t/getting-current-time/2434/4 "2018-11-03T16:36:20Z")

</div>

Here is small example in Ellie about how to add items to `Dict` on button press, using current time as key: [https://ellie-app.com/3N8Pk6BZy39a1](https://ellie-app.com/3N8Pk6BZy39a1)

---

<div class="post-metadata">

**Author:** ![malaire](https://avatars.discourse-cdn.com/v4/letter/m/b782af/32.png) [@malaire](https://discourse.elm-lang.org/u/malaire)\
**Post date:** [November 3, 2018, 4:52pm UTC](https://discourse.elm-lang.org/t/getting-current-time/2434/5 "2018-11-03T16:52:40Z")

</div>

> [@stevensonmt](#):
>
> My problem is not with the Time module per se, but with Tasks in general. I don’t understand the need for them and what they accomplish. When I say “Elm, I want the current time” I expect Elm to return the current time.

Functions in Elm are pure functions without any side effects. Only information functions can access is their parameters and nothing else. So it’s impossible to create a function `getCurrentTime` in Elm as functions can never access the clock.

Only Elm runtime has access to clock, which is why you need a `Task` which gets the time from Elm runtime.

---

<div class="post-metadata">

**Author:** ![stevensonmt](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/stevensonmt/32/889_2.png) [@stevensonmt](https://discourse.elm-lang.org/u/stevensonmt)\
**Post date:** [November 5, 2018, 3:58pm UTC](https://discourse.elm-lang.org/t/getting-current-time/2434/6 "2018-11-05T15:58:35Z")

</div>

Thank you for the illustrative example, very helpful. I guess I’m still just confused by the need to expose that need for the `Task`. Are there any instances in which you would call `Time.now` and not perform a `Task` of some sort? If not, why not have the `Time` module build that in?

---

<div class="post-metadata">

**Author:** ![malaire](https://avatars.discourse-cdn.com/v4/letter/m/b782af/32.png) [@malaire](https://discourse.elm-lang.org/u/malaire)\
**Post date:** [November 5, 2018, 4:13pm UTC](https://discourse.elm-lang.org/t/getting-current-time/2434/7 "2018-11-05T16:13:09Z")

</div>

No, I don’t think there is any case when you would use `Time.now` without `Task`.

But there are other use cases for `Time.now` than just simply getting current time. For example in my time-syncing app I use three tasks together:

1. get request start time (`Time.now`)
2. fetch time from server (`Http.get`)
3. get request end time (`Time.now`)

And then I calculate client/server clock offset from these values.

So since there are different ways to use tasks like `Time.now`, it makes sense to put all common `Task` related code in it’s own package and not complicate for example `Time` package with such code, even if that does make simple case of e.g. getting current time a bit more complicated that it needs to be (i.e. must use `Task.perform` with `Time.now`).

---

<div class="post-metadata">

**Author:** ![joelq](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/joelq/32/445_2.png) [@joelq](https://discourse.elm-lang.org/u/joelq)\
**Post date:** [November 5, 2018, 4:35pm UTC](https://discourse.elm-lang.org/t/getting-current-time/2434/8 "2018-11-05T16:35:44Z")

</div>

> [@stevensonmt](#):
>
> Are there any instances in which you would call `Time.now` and not perform a `Task` of some sort?

In Elm, _all functions must return the same value given the same arguments_. Functions that have this property are called **pure functions** and in Elm all functions must be pure.

Functions like `increment` are pure. You can call `increment 2` as many times as you want in any context and you will always get `3` back.

The problem comes with functions like `rand` or `now`. The entire purpose of these functions is to return different values every time they are called. These functions would be useless if they always returned the same value. However, to have functions like that would violate Elm requirement that all functions must be pure.

Elm’s solution is an approach called **effects as data**. Instead of actually doing the work of calculating a random value or the current time ourselves, we generate a “request” for the Elm runtime to do that work. This “request” (a `Task` or `Cmd` value) _does not immediately execute_. Instead it is a piece of data that can be returned, passed around, and transformed. Eventually we pass it to the Elm runtime (by returning it from the root `update` function) and the runtime will execute it.

So what does all this fanciness this buy us? `Time.now` and `Random.generate` now always return the _same_ value (the “request” to the runtime) for the same arguments. `Time.now` always returns the same task. We’ve changed non-pure functions into pure functions!

---

<div class="post-metadata">

**Author:** ![stevensonmt](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/stevensonmt/32/889_2.png) [@stevensonmt](https://discourse.elm-lang.org/u/stevensonmt)\
**Post date:** [November 5, 2018, 5:50pm UTC](https://discourse.elm-lang.org/t/getting-current-time/2434/9 "2018-11-05T17:50:18Z")

</div>

I guess this is a case in which ideological purity is at odds with the secondary goal of ease of use. Thanks for the explanation.

---

<div class="post-metadata">

**Author:** ![stevensonmt](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/stevensonmt/32/889_2.png) [@stevensonmt](https://discourse.elm-lang.org/u/stevensonmt)\
**Post date:** [November 5, 2018, 5:53pm UTC](https://discourse.elm-lang.org/t/getting-current-time/2434/10 "2018-11-05T17:53:23Z")

</div>

Is your app code public? I’d like to look through it if so. Thanks for taking the time to help me out.

---

<div class="post-metadata">

**Author:** ![malaire](https://avatars.discourse-cdn.com/v4/letter/m/b782af/32.png) [@malaire](https://discourse.elm-lang.org/u/malaire)\
**Post date:** [November 5, 2018, 6:05pm UTC](https://discourse.elm-lang.org/t/getting-current-time/2434/11 "2018-11-05T18:05:10Z")

</div>

> [@stevensonmt](#):
>
> Is your app code public? I’d like to look through it if so. Thanks for taking the time to help me out.

Yes, it’s “time-check” at [elm-examples](https://github.com/malaire/elm-examples) ([Main.elm](https://github.com/malaire/elm-examples/blob/master/time-check/src/Main.elm)). Live version is [here](https://www.markuslaire.com/github/elm-examples/time-check/index.html).

---

<div class="post-metadata">

**Author:** ![Brian\_Carroll](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/brian_carroll/32/444_2.png) [@Brian\_Carroll](https://discourse.elm-lang.org/u/Brian_Carroll)\
**Post date:** [November 5, 2018, 6:36pm UTC](https://discourse.elm-lang.org/t/getting-current-time/2434/12 "2018-11-05T18:36:34Z")

</div>

> [@stevensonmt](#):
>
> I guess this is a case in which ideological purity is at odds with the secondary goal of ease of use.

Yeah true. But all the good stuff about Elm (no runtime exceptions, easy refactoring, the feeling of “if it compiles it works”) comes from its guarantees. And guarantees have to be absolute or they don’t work. To quote one of Richard Feldman’s talks, “just one drop if urine in a barrel of water gives you a barrel of urine-water”.  
The trade-off is that you get some simple cases that feel overly complicated. Time and randomness are the two main ones I can think of.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/1X/50a05e53677a2c3b47776d7abd0f113eb50193a1.png) [@system](https://discourse.elm-lang.org/u/system)\
**Post date:** [November 15, 2018, 6:36pm UTC](https://discourse.elm-lang.org/t/getting-current-time/2434/13 "2018-11-15T18:36:47Z")

</div>

Closed on November 15, 2018, 6:36pm UTC

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.
