我坚持从榆树日期获取当前时区偏移。
Date.now
返回
<Thu Feb 22 2018 20:42:42 GMT+0530 (India Standard Time)>
为字符串 正如我在Elm的日期和时间核心库中探讨的那样,他们没有提供任何直接的方法来获取当前时区偏移量。那我该怎么办?
import Html as App
import Html exposing (..)
import Date exposing (Date)
import Task
type alias Model =
Maybe Date
type Msg =
SetDate (Maybe Date)
update : Msg -> Model -> (Model, Cmd Msg)
update (SetDate date) _ =
(date, Cmd.none)
view : Model -> Html Msg
view model =
div [] [ text <| dateString model ]
dateString : Model -> String
dateString model =
case model of
Nothing -> "No date here"
Just date ->
(toString <| date)
now : Cmd Msg
now =
Task.perform (Just >> SetDate) Date.now
main : Program Never Model Msg
main =
App.program
{ init = ( Nothing, now )
, view = view
, subscriptions = always Sub.none
, update = update
}
我需要这个
+0530
,就像在浮动5.5
中一样。
答案 0 :(得分:6)
榆树的DateTime函数目前非常稀疏,但是只是为了解决这类问题,请尽量减少Date.Extra库。查看here
您可以将其导入,
import Date.Extra exposing (offsetFromUtc)
然后,您将toString <| date
管道更改为
date
|> offsetFromUtc
|> toString
如果你想要浮点值,那么它会在几分钟内给你你的偏移量,只需将int除以60.这里的简单函数是这样做的:
divBy60 : Int -> Float
divBy60 t =
toFloat t / 60.0
然后再次将管道更改为
date
|> offsetFromUtc
|> divBy60
|> toString