如何在 Elm 中显示 GMT 中的当前时区偏移量
How can I display current Timezone offset in GMT, in Elm
I'm stuck on getting the current Timezone offset from the Date in Elm.
Date.now
This returns
<Thu Feb 22 2018 20:42:42 GMT+0530 (India Standard Time)>
as string
As I have explored in Elm's core lib of date and time, and they don't provide any direct method to fetch the current timezone offset. So what should I do?
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
}
I need this +0530
as in the float 5.5
.
Elm 的 DateTime 函数目前非常少,但 justinmimbs 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
I'm stuck on getting the current Timezone offset from the Date in Elm.
Date.now
This returns
<Thu Feb 22 2018 20:42:42 GMT+0530 (India Standard Time)>
as string As I have explored in Elm's core lib of date and time, and they don't provide any direct method to fetch the current timezone offset. So what should I do?
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
}
I need this
+0530
as in the float5.5
.
Elm 的 DateTime 函数目前非常少,但 justinmimbs 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