How do i defualt a date model variable to todays date

I have the model set as follow

type alias Model =
    { currentdate : Maybe Date }
init : Model
init =
    { currentdate = Just Date}

I am expecting the above to set the current date variable to todays date but i get an error

Cannot find variable `Date`

currentDate = Just Date

I haven’t dealt with dates yet but I’m gonna assume you’re using this package.

The issue here is that you’re using Date (type) as a value. I highly recommend reading more about Elm’s types, and the differences between type constructors and data constructors for union types.

One way you can obtain the date is to pass it as an input argument from JS, as shown here.

You can also use function from the same package to perform a task in the init function, as seen here.

Conclusion: obtaining the date/time is an async operation (AKA side effect), and Elm, being a purely functional language, deals with them using the commands.

In addition to hrnxm comments, typically in Elm you can have a reasonable precise date/time value by having a “subscription” (see Commands and Subscriptions · An Introduction to Elm) that runs periodically and ask the system for current date/time.

That way, you store the value in your Model.currentDate and use wherever you want in your app. Also, to avoid having to deal with Maybe's you can have a init value passed from JS (see reply above).

When I started with Elm, I‘ve run into a similar problem: The init function runs only once, so why bother with tasks and subscriptions to do a simple thing like getting today’s date—especially because you can be fairly certain that the date doesn’t change during one session?

That put me off a little since in other languages it’s such a simple operation. The key for me was to realize that:

a) There is actually no guarantee that the init function is called only once. It’s perfectly fine to call it later from my own code, e.g. to reset the model to an initial state. And then the guarantee that functions always return the same values when called with the same parameters (none in this case) would be broken.

b) The date might actually change during one session. It happens every day. So why would my code assume that it doesn’t?

Keeping all functions side-effects free gives me all the power of Elm and the downsides of making init (or Date) special simply aren’t worth it.

Maybe it’s obvious or long winded. I’m just recounting my thought process back when I began using Elm. Hope it helps.


Another option if you’re starting out learning Elm and want to carry on quickly:

import Date
import Time exposing (Month(..)) -- comes from `elm install elm/time`

init : Model
init =
    { currentdate = Date.fromCalendarDate 2022 Jan 15 }

This will get you a hardcoded date value, but it might be helpful if you want to limit what you’re learning and just keep going right now.

If you are learning it’s totally okay to work up to to things like tasks/ports later!

1 Like

Another solution is using flags for this.
What we do is pass the time as a flag and later on get a Date from that

In JS :

let flags = { initialTime: (new Date()).getTime() }
let app = Elm.App.init({ node, flags })

In Elm

type alias Flags =
    initialTime : Posix

Decoding the flags looks like:

timeDecoder =
  |> round
  |> Time.millisToPosix Flags
  Decode.field "initialTime" timeDecoder

And later when needed we convert the time to a Date.

Date.fromPosix Time.utc model.flags.initialTime

Just a small correction, I think you meant:

type alias Flags =
    initialTime : Int

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