Question

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

Was it helpful?

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top