【问题标题】:Converting Ecto.DateTime to Erlang datetime tuple using to_erl使用 to_erl 将 Ecto.DateTime 转换为 Erlang 日期时间元组
【发布时间】:2017-03-02 14:39:36
【问题描述】:

我有一个 Ecto.DateTime 我正在尝试从中提取信息。

这工作正常:

{{y, m, d}, _} = Ecto.DateTime.to_erl(date)
"#{m}/#{d}/#{y}"

我现在正在尝试获取小时/分钟/秒值:

{{y, m, d}, {h,m,s}} = Ecto.DateTime.to_erl(date)
"#{m}/#{d}/#{y}"

但我得到了这个错误

右侧值不匹配:{{2017, 5, 5}, {12, 0, 0}}

【问题讨论】:

    标签: datetime elixir ecto


    【解决方案1】:

    您在模式中重复使用变量名称m,这意味着这仅在月份和分钟值相同时才有效。您需要使用不同的名称,例如

    {{y, m, d}, {h, min, s}} = Ecto.DateTime.to_erl(date)
    

    {{y, mon, d}, {h, m, s}} = Ecto.DateTime.to_erl(date)
    
    iex(1)> {a, a} = {1, 2}
    ** (MatchError) no match of right hand side value: {1, 2}
    
    iex(1)> {a, a} = {1, 1}
    {1, 1}
    

    【讨论】:

    • 天啊!好收获!
    • @SergioTapia 请考虑将有帮助的答案标记为正确。
    猜你喜欢
    • 2019-01-26
    • 2010-10-23
    • 2019-04-21
    • 2012-09-06
    • 2018-04-24
    • 2011-12-06
    • 1970-01-01
    • 2012-07-29
    相关资源
    最近更新 更多