使用to_erl将Ecto.DateTime转换为Erlang datetime元组

时间:2017-03-02 06:37:37

标签: datetime elixir ecto

我有一个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}}

1 个答案:

答案 0 :(得分:4)

您在模式中重复使用变量名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}
相关问题