How to Convert Unix Timestamp to DateTime using Apex in Salesforce

Our Salesforce org has an application that logs user login events, including a Unix timestamp indicating the exact time each login occurred.

These logs are sent to the org to analyze user activity and track engagement over time. We had to convert the Unix timestamp for each login event into a DateTime format for reports and other analysis purposes.

In this Salesforce tutorial, I will explain how to convert a Unix Timestamp to a DateTime using Apex in Salesforce.

What is a UNIX timestamp in Salesforce?

In Salesforce, a UNIX timestamp is a way of representing time as a large number, counting the number of seconds that have passed since January 1, 1970, at 00:00:00 UTC. It’s a standard format used in many systems to track dates and times consistently.

In Salesforce, we generally use UNIX timestamps while working with APIs or integrating with other systems. To use it, you can convert it to a regular date and time by using Salesforce’s built-in functions in Apex or formulas to transform the timestamp from a format like 1716435835 into a readable format, such as 03:45:33.

Convert Unix Timestamp to DateTime using Apex in Salesforce

To convert Unix Timestamp to Datetime in Apex, we will use the DateTime class and the newInstance(milliseconds) method.

Now, navigate to the Salesforce developer console and follow the code structure below to convert Unix Timestamp to DateTime in Apex.

Long unixTime = 1716435835;
System.debug('unixTime: ' + unixTime);
Long unixTimeMilliseconds = unixTime * 1000; 
DateTime dateTimeValue = DateTime.newInstance(unixTimeMilliseconds);
System.debug('dateTimeValue: ' + dateTimeValue);

After this, execute the above apex anonymous window code. Then, in the execution log window, you will see the UNIX timestamp and the Datetime values.

Convert UNIX time to Date time in Salesforce Apex

This way, we can convert UNIX timestamp values to Datetime in Salesforce Apex using the DateTime.newInstance() method.

Convert UNIX Timestamp to DateTime According to the Locale in Salesforce Apex

In Salesforce, datetimes are always stored as a timestamp in the GMT/UTC time zone. Salesforce uses the locale set in your user profile to adjust the displayed datetime to match your local time zone.

Now, we will explore a method for converting a UNIX Timestamp to a datetime according to the local time in Salesforce Apex.

Launch the Apex anonymous code window, enter the code below, and then execute it.

Long timestamp = Long.valueOf('1510654220787');

DateTime gmtDatetime = DateTime.newInstance(timestamp);

System.TimeZone myTz = UserInfo.getTimeZone();

Integer millisecondOffsetGmt = myTz.getOffset(gmtDateTime);

DateTime localDatetime = DateTime.newInstance(timeStamp - millisecondOffsetGmt);

system.debug(gmtDatetime);  
system.debug(localDatetime);

In the execution log window, you will see the UNIX timestamp value converted to a datetime in GMT and the local date and time.

How to convert UNIX time to Datetime in Salesforce Apex

This way, we can convert UNIX Timestamp to datetime according to the Locale in Salesforce Apex by following the above steps.

Convert UNIX Timestamp to DateTime Using System.now() method

To convert a UNIX timestamp to a datetime using the System.now() method, open the Salesforce Developer Console and launch the Apex Anonymous Window.

Enter the code below in the Apex window and execute it.

DateTime currentDT = System.now();
String strUnixTimeStamp = String.valueOf(currentDT.getTime());
System.debug('String UnixTimeStamp is ' + strUnixTimeStamp);
Long longUnixTimeStamp = Long.valueOf(strUnixTimeStamp);
System.debug(DateTime.newInstance(longUnixTimeStamp));

Now, in the execution log window, we will get the datetime values converted from the UNIX timestamp.

This way, we can convert the Current Timestamp to a DateTime using the System.now() method by following the above steps.

Conclusion

In this Salesforce tutorial, we have learned about the various methods for converting a UNIX timestamp to a datetime. For conversion, we used methods such as newInstance(), Datetime(), and to obtain the converted value from UNIX time to datetime according to the local time, we utilized the UserInfo.getTimeZone() method.

You may also like to read.

Agentforce in Salesforce

DOWNLOAD FREE AGENTFORCE EBOOK

Start with AgentForce in Salesforce. Create your first agent and deploy to your Salesforce Org.

Salesforce flows complete guide

FREE SALESFORCE FLOW EBOOK

Learn how to work with flows in Salesforce with 5 different real time examples.