如何获得榆木的当前日期?

编程入门 行业动态 更新时间:2024-10-24 20:11:32
本文介绍了如何获得榆木的当前日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图弄清楚如何在.17版本的Elm中获取当前日期。我看到他们在.17中添加了Date模块,但是我还没有找到任何有关如何使用它的示例。有没有人知道如何做到这一点?

I'm trying to figure out how to get the current date in elm in version .17. I see that they added a Date module to .17 but I haven't found any examples on how it's used. Has anyone figured out how to do this?

编辑:在尝试改进此解决方案时,我遇到了另一个绊脚石。我试图触发设置日期,然后打电话给另一个Msg做其他事情。但是我仍然得到{}作为约会对象。

In trying to retrofit this solution, I hit another stumbling block. I'm trying to trigger setting the date and then calling another Msg to do something else. But I'm still getting {} for a date.

import Html.App as App import Html exposing (..) import Time exposing (Time) import Task import Date exposing (Date) import Html.Events exposing (onClick) import Html.Attributes exposing (..) type alias Model = {currentDate : Maybe Date} type Msg = SetDate (Maybe Date) | TriggerDateSet update : Msg -> Model -> (Model, Cmd Msg) update msg model = case msg of SetDate date -> ({model | currentDate = date}, Cmd.none) TriggerDateSet -> (model, now) view : Model -> Html Msg view model = div [] [ div [] [ button [onClick TriggerDateSet] [] ] , div [] [ text <| "(Optional) time at program launch was " ++ toString model ] ] now : Cmd Msg now = Task.perform (always (SetDate Nothing)) (Just >> SetDate) Date.now main : Program Never main = App.program { init = ( Model Nothing, now ) , view = view , subscriptions = always Sub.none , update = update }

推荐答案

您将需要 now 任务或 订阅 rel = nofollow noreferrer>时间。

You'll want the now Task or the every Subscription from Time.

下面是一个使用前者以当前时间初始化模型的示例。

Here's an example using the former to initialise the model with the current time.

import Html.App as App import Html exposing (..) import Time exposing (Time) import Task type alias Model = Maybe Time type Msg = SetTime (Maybe Time) update : Msg -> Model -> (Model, Cmd Msg) update (SetTime time) _ = (time, Cmd.none) view : Model -> Html Msg view model = div [] [ text <| "(Optional) time at program launch was " ++ toString model ] now : Cmd Msg now = Task.perform (Just >> SetTime) Time.now main : Program Never main = App.program { init = ( Nothing, now ) , view = view , subscriptions = always Sub.none , update = update }

更多推荐

如何获得榆木的当前日期?

本文发布于:2023-10-31 07:20:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545416.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:榆木   如何获得   日期

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!