gpt4 book ai didi

date - 如何在 Elm 中以格林威治标准时间显示当前时区偏移量

转载 作者:行者123 更新时间:2023-12-05 00:13:09 24 4
gpt4 key购买 nike

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

关于date - 如何在 Elm 中以格林威治标准时间显示当前时区偏移量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48941655/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com