Unix Time to Current Date/Time

How do I convert Unix time to human readable date/time?

The documentation for the API I’m using states “We report all times in UTC, and our API uses Unix time as a standard”

I received 1390530803 as a timestamp which equates to Fri, 24 Jan 2014 02:33:23 GMT

Xojo and UNIX Time

Thanks Dave. I did find that post but it explains how to take a date object or human readable date and convert it to unix time (UTC). I’m trying to figure out how to convert a unix timestamp (i.e. 1390530803) to a human readable date (Fri, 24 Jan 2014 02:33:23 GMT).

Simply add instead of subtracting.

You can use a method similar to the following one to convert times stored as integers (e.g. unix UTC times) into a human readable format:

Function UnixTimeToString(seconds As UInt64) As String
  Dim d As new Date
  
  d.TotalSeconds = seconds + 2082844800
  
  return str(d)
End Function

This can then be used throughout the program as:

MsgBox UnixTimeToString(1390530803)

If you wanted to change the default string format that your program uses to display dates in, you simply update the UnixTimeToString with the desired format (e.g. instead of using Str(d)).

That could be improved a little by returning a date instead of a String :slight_smile:

Function UnixTimeToString(seconds As UInt64) As Date
  Dim d As new Date
  
  d.TotalSeconds = seconds + 2082844800
  
  return d
End Function

Called like this instead :slight_smile:

MsgBox UnixTimeToString(1390530803).SQLDateTime

Perhaps one should also rename the method to “UnixTimeToDate” to avoid confusion. :wink:

Also, UTC means Universal Time Coordinated, not Unix time code… You need to add or subtract your timezone offset from the number provided.

AH the joy of dates
IF and only IF daylight is in effect on the date given (not today)
And then it has to be “the right offset” for the TZ (yay!) and some are full, half and quarter hours and the right one for the year since over the years they have changed
http://en.wikipedia.org/wiki/List_of_UTC_time_offsets

Sorry to bump such an old thread but, I experienced some pain using this process.

Should be noted that a standard integer will not work and will loop back into negative numbers. Your value needs to be an int64

Hope this helps,
-JH