Use Case Accelerators > How to Convert a UNIX (or POSIX) Timestamp to a DateTime Value

How to Convert a UNIX (or POSIX) Timestamp to a DateTime Value

Article #: Product: Version:

Summary

The DMExpress DateAdd function can be used to convert a UNIX (or POSIX) timestamp to a DateTime value. UNIX timestamps are given in UTC (Universal Coordinated Time), so the converted value will also be in UTC.

Resolution

The attached example demonstrates how to convert a UNIX (or POSIX) timestamp to a DateTime value.

UNIX time is defined as the number of seconds that have elapsed since Unix Epoch Time, which is midnight Coordinated Universal Time (UTC), 1 January 1970.

The example task defines the following values:

UNIX_Epoch_Time represents Unix Epoch Time as a DateTime value using the ToDate function
UNIX_To_DateTime converts a given UNIX timestamp to a DateTime value by adding the UNIX time (whose unit is seconds) to the UNIX_Epoch_Time using the DateAdd function

For example, given an input Unix time of 1362612890, the value of UNIX_To_DateTime would be 2013-03-06 23:34:50 (in UTC).

The format of the DateTime value obtained from the conversion can be changed by changing the date mask of the value UNIX_Epoch_Time. If you want to convert the DateTime value to your local time zone, you would need to add/subtract the appropriate number of hours relative to UTC.

Attachments

177_ConvertUnixTimeToDateTime.zip, compatible with DMExpress version 7.6 or higher

Additional Information

For additional information on the ToDate and DateAdd functions, and date masks, see ToDate function, DateAdd function, and Syntax of date pattern in the DMExpress Help.

Last updated: