How to convert a UnixTimeStamp to a datetime

Unix Timestamps are often used in Web services to show specific times. For instance when an object was created or updated.

There are some undocumented functions to get your Date from a Unix timestamp, especially with the new C# plugin on Linx, however, I find that if you understand the reason behind the timestamp, its even easier to get the correct date from it.

So, the first question is, what is a Unix timestamp. When you see them, they look like large numbers. For instance:

{
“Created”:1621324473
}

It is exactly that. A large number of seconds, since mid-night, 1970-01-01

Thus, to get your date from this number, you can simply create a Date object for the date 1970-01-01 and add on the seconds from the integer.

Here’s an example:

Screenshot 2021-05-19 at 09.23.49

  • I’ve got “IntegerTimeStamp” with the value 1621324473:

Screenshot 2021-05-19 at 09.24.49

  • Next I’ve got a Date “DateTime19700101” with the value “1970-01-01 00:00:00”:

Screenshot 2021-05-19 at 09.25.56

  • Finally I just add the seconds from my “IntegerTimeStamp” to my “DateTime19700101”:

  • And I get my result:


To summarise, you can do all of this in 1 line in your expression editor, assuming you are getting the UnixTimeStamp from somewhere, either a REST Call or Parameters for your function, etc:

“1970-01-01”.ToDateTime(“yyyy-MM-dd”).AddSeconds(UnixTimeStamp).ToString(“yyyy-MM-dd HH:mm:ss.fffffff”)


Added Bonus: How do you create an Unix Timestamp for the current Date and time:

It basically works the same way. You take the current date/time and you calculate how many seconds since 1970-01-01 (Thanks @Franz for the info)

($.System.CurrentDateTime - “1970-01-01”.ToDateTime(“yyyy-MM-dd”)).TotalSeconds

Another example solution showing how this can be done for normal Unix Dates and Millisecond Unix Dates:
Linx5 UnixDateExamples.lsoz (6.9 KB)
Linx6.0.5 Linx6.zip (14.4 KB)

For clarification, and to help with searches: Unix time or UnixTimeStap is also referred to as:

  • Epoch Time
  • Epoch Date
  • Epoch Time Stamp
  • POSIX time
  • Time since the Epoch
  • Seconds since 1970
  • Unix epoch
  • 1970 timestamp