Convert timestamp to datetime in erlang

落爺英雄遲暮 提交于 2019-12-18 11:45:10

问题


How can I convert a timestamp (number of milliseconds since 1 Jan 1970...) to Date or DateTime format in Erlang? Something like {Year,Month,Day}.


回答1:


Roughly:

msToDate(Milliseconds) ->
   BaseDate      = calendar:datetime_to_gregorian_seconds({{1970,1,1},{0,0,0}}),
   Seconds       = BaseDate + (Milliseconds div 1000),
   { Date,_Time} = calendar:gregorian_seconds_to_datetime(Seconds),
   Date.



回答2:


It just so happens that I have a github gist with a bunch of datetime utilities for exactly this purpose: http://gist.github.com/104903. Calendar has most of the low level plumbing for this stuff.



来源:https://stackoverflow.com/questions/825151/convert-timestamp-to-datetime-in-erlang

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!