datetimeerlang

Convert timestamp to datetime in erlang


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


Solution

  • 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.