【发布时间】:2019-04-30 04:34:53
【问题描述】:
我有两个函数用于将 ISO 日期和 ISO 时间字符串格式化为常规字符串,例如“1999-10-12” -> “12/10/1999” & “18:00:00” -> “6:00pm”,它们涉及到很多转换函数...
我想知道是否有更高效的方式来格式化 ISO 字符串输入:
# ISO Date String to DD/MM/YYYY
def date_to_dd_mm_yyyy(iso_date) do
{_, date} = Date.from_iso8601(iso_date)
{year, month, day} = Date.to_erl(date)
Integer.to_string(day) <> "/" <> Integer.to_string(month) <> "/" <> Integer.to_string(year)
end
# ISO Time String to 12 hour time
def time_to_12hour(iso_time) do
{:ok, new_time} = Time.from_iso8601(iso_time)
{hour, minute, _second} = Time.to_erl(new_time)
minute_string =
cond do
minute < 10 ->
"0" <> Integer.to_string(minute)
true ->
Integer.to_string(minute)
end
hour_string =
cond do
hour === 0 ->
"12"
hour > 12 ->
Integer.to_string(hour - 12)
hour <= 12 ->
Integer.to_string(hour)
end
meridiem =
cond do
hour >= 12 ->
"pm"
hour < 12 ->
"am"
end
hour_string <> ":" <> minute_string <> meridiem
end
【问题讨论】:
标签: performance date time elixir