使用 to_erl 将 Ecto.DateTime 转换为 Erlang 日期时间元组
Converting Ecto.DateTime to Erlang datetime tuple using to_erl
我有一个 Ecto.DateTime 我正在尝试从中提取信息。
这工作正常:
{{y, m, d}, _} = Ecto.DateTime.to_erl(date)
"#{m}/#{d}/#{y}"
我正在尝试获取 hours/minute/second 值:
{{y, m, d}, {h,m,s}} = Ecto.DateTime.to_erl(date)
"#{m}/#{d}/#{y}"
但是我得到这个错误
no match of right hand side value: {{2017, 5, 5}, {12, 0, 0}}
您在模式中重复使用了变量名称 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}
我有一个 Ecto.DateTime 我正在尝试从中提取信息。
这工作正常:
{{y, m, d}, _} = Ecto.DateTime.to_erl(date)
"#{m}/#{d}/#{y}"
我正在尝试获取 hours/minute/second 值:
{{y, m, d}, {h,m,s}} = Ecto.DateTime.to_erl(date)
"#{m}/#{d}/#{y}"
但是我得到这个错误
no match of right hand side value: {{2017, 5, 5}, {12, 0, 0}}
您在模式中重复使用了变量名称 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}