Unix seconds to date time?


What's the best method to convert unix timestamp to DateTime?

I've been trying this method, but no success.

AddSeconds(NewDateTime(1970,1,1,0,0,0), DecimalToInteger(UnixTimestamp))

Hi Daniel,

What are you getting?

The wrong value or not working at all?

It should kind of work, but you probably still missing the timezone and daylight savings since the platform date times are all LocalTimes and not UTC.

Look at this piece of code that I copied from the platform REST API's code (that does this type of conversion automatically). You can easily place it in an extension:


           long t = long.Parse(source);

           return new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(t).ToLocalTime();

            try {
                long secondsSinceEpoch = Long.parseLong(source);
                Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
                cal.setTime(new Date(secondsSinceEpoch * 1000));
                return cal.getTime();
            } catch (NumberFormatException e){
                throw new IllegalArgumentException("The value '" + source + "' is not valid for Unix DateTime.");

João Rosado

João Rosado


Try it like this: AddSeconds(NewDateTime(1970,1,1,0,0,0),UnixTimestamp)