The organization where I work as a Salesforce developer has a global Salesforce implementation with users in multiple time zones. When scheduling tasks or reminders, it’s crucial to ensure that each user creates these events at the correct local time.
When a user logs in to Salesforce, the system must display the current time and date adjusted to their local time zone. Additionally, tasks created should reflect the user’s local time, not the server time.
As a solution, I implemented an Apex class using the showCurrentTime() function to calculate the user’s local time based on their time zone. The code will get the user’s time zone, the current date, and the time and then adjust the time according to the user’s time zone.
Let’s learn how to get the current datetime in Salesforce using Apex code.
Get Current Datetime in Apex Salesforce
To get the current datetime value in Salesforce using Apex, navigate to the Salesforce developer console and follow the steps below.
- In this approach, we will create an Apex class. To do so, click on File > New > Apex Class.
- Enter the Name of the new class and click OK.

- Enter the code below in the developer console to get the current datetime.
public class GetCurrentDateTime {
public static void showCurrentTime() {
DateTime now = DateTime.now();
Integer hours = now.hour(), minutes = now.minute(), seconds = now.second();
System.debug('Current DateTime: ' + now);
System.debug('Current Time: ' + hours + ':' + minutes + ':' + seconds);
}
}- Now, I will break down the above code for better understanding.
- In the above code, I created an Apex class named GetCurrentDateTime and used its static void method, indicating that the method belongs to the class rather than an instance and doesn’t return a value.
- In the third row, I used the DateTime.now() function to fetch the current datetime.
- To get the GMT time, I’ve used (‘Current DateTime: ‘ + now), and to display my system’s current time, I have used (‘Current Time: ‘ + hours + ‘:’ + minutes + ‘:’ + seconds).
- After entering the code, press Ctrl + E to execute it.
- In the Anonymous code execution window, enter the ‘class name.method name();’ as shown in the image below, then select the checkbox Open Log and click Execute.

- In the console log, we can see the two time zones in the Display column.
- System.debug shows the GMT value in the first row, and by calling hour(), minute(), and second(), I received the local time in the second row.

The current time displayed using any time or datetime function will always be in GMT, not according to your current time zone.
Following the above steps, we can get the current datetime in Salesforce Apex using the DateTime.now() function.
Get the Current Datetime of a Specific TimeZone in Apex Salesforce
In Salesforce Apex, we can get the datetime of a specific timezone, but we need to adjust it to another time zone by using the TimeZone class in our code.
The TimeZone class is typically used to convert a user’s input from a local time zone into GMT since the entire platform runs on GMT, not on users’ time zones.
To learn how to get the current datetime of a specific timezone, navigate to the developer console and follow the steps below.
- In the developer console, click on the File > New > Apex Class.
- Enter the Name for the apex class and click the OK button.

- In this apex class, we will get the current datetime of New York, USA. Now, enter the code below in the developer console.
public class GetSpecificDateTime {
public static void showCurrentTime() {
TimeXone tz = TimeZone.getTimeZone('America/New_York');
DateTime dt = DateTime.now();
System.debug('Actual Time as String: ' + dt.format());
System.debug('Formatted Time: ' + dt.addSeconds(tz.getOffset(dt) / 1000));
}
}- Below is the explanation of the code we used in the apex class.
- In the Apex class, we have used the Static void method named showCurrentTime(), which means it can be called without creating an instance of the class.
- TimeZone tz = TimeZone.getTimeZone(‘America/New_York’) – Creates Timezone object tz for the time zone “America/New_York“. This object will compute the time zone offset from UTC (Coordinated Universal Time).
- DateTime dt = DateTime.now() – retrieves the current date and time in the Salesforce server’s default time zone and stores it in the Datetime variable dt.
- After entering the code, save it and press Ctrl + E to execute it in the anonymous window. In that window, enter the class name.method name(), and then press the execute button.

- In the execution log window, we can see the current server time and the New York, USA timezone.

- System.debug(‘Actual Time as String: ‘ + dt.format()); prints the current server date and time in a default formatted string.
- System.debug(‘Formatted Time: ‘ + dt.addSeconds(tz.getOffset(dt) / 1000)); calculates the time zone offset in milliseconds for the “America/New_York” time zone at the given Datetime dt.
This way, we can find the current datetime of any specific time zone using the imeZone.getTimeZone() in Salesforce Apex.
Get the Current DateTime of the Logged User in Apex Salesforce
To get the current Datetime of the logged user in Salesforce Apex, we can use the UserInfo.getTimeZone() function.
In the steps below, I will show you how to use the above function in your Salesforce apex code to get the current date and time of the logged-in user.
Now, navigate to the Salesforce developer console and follow the steps below.
- In the developer console, click on the File > New > Apex Class.
- Enter the Name for the apex class and click the OK button.

- Enter the code below in the developer console and save it.
public class UserDateTime {
public static void showUserCurrentTime() {
TimeZone tz = UserInfo.getTimeZone();
DateTime dt = DateTime.now();
System.debug('Actual Time as String: ' + dt.format());
}
}- After saving the code, execute it using “Ctrl+E” keys. In the anonymous window, enter ” ClassName.MethodName();“. Next, click the Open Log button, then click the Execute button.

- In the console log window, select the checkbox Debug only. Then, it will display the logged-in user’s current datetime. Currently, it is showing the current local time according to my time zone.

This way, we can display the current datetime according to the local time zone of the logged-in user using the UserInfo.getTimeZone() in Salesforce apex.
Conclusion
In this Salesforce tutorial, we have learned how to get the current datetime in Salesforce Apex. We have learned to get the current datetime for your system and then the current datetime of the specific timezone. We have also displayed the GMT zone to show that the datetime values are always in GMT, and we have to customize it with additional functions to get the desired datetime.
You may also like to read:
- Convert a String to an Integer using Apex in Salesforce
- Variables and Data Types in Salesforce Apex
- Convert String to Date in Salesforce Apex
- Logical and Looping Statements in Salesforce Apex Programming

Abhijeet is a skilled Salesforce developer with experience in developing and integrating dashboards, data reports, and Salesforce applications. He is also skilled at optimizing processes and flow automation processes, coding, and executing complex project architecture. Read more about us | LinkedIn Profile.