Erlang 将时间戳转换为 Year-Month-DayTHour:Min:SecZ 格式

Erlang converting timestamp to Year-Month-DayTHour:Min:SecZ format

您好,我想转换 erlang:now()。时间戳输出:

> erlang:now().
{1425,589373,955614}

转换成年-月-日THour:Min:SecZ格式。最快的方法是什么?

不确定这是最快的方式,但我会试试这个:

{{Year, Month, Day}, {Hour, Minute, Second}} = calendar:now_to_datetime(erlang:now()),
StrTime = lists:flatten(io_lib:format("~4..0w-~2..0w-~2..0wT~2..0w:~2..0w:~2..0w",[Year,Month,Day,Hour,Minute,Second])).

您感兴趣的格式称为 ISO 8601. If you're looking for more than a quick conversion, I've liked this library from Sean Sawyer: https://github.com/seansawyer/erlang_iso8601

您可以使用特殊的库来解析和格式化日期。例如 tempo or qdate.

erlsci iso8601 库在格式化(和解析)日期方面做得很好。