In our Salesforce org, we have a custom form field where users manually enter the date and time for follow-up meetings. The input is captured as a string from a custom form field.
We need to convert this string input into a Datetime format so that we can create a follow-up event in the system in the GMT zone.
In this Salesforce tutorial, I explain possible ways to convert a string to a datetime in Salesforce Apex.
Convert a String to a Datetime using the valueOf() method in Salesforce Apex
In Salesforce Apex, we convert a string to a datetime using the valueOf() method or DateTime.valueOf(dateString.
Navigate to the Salesforce developer console and follow the steps below.
- In this approach, we will create an Apex class by selecting File > New > Apex Class.
- Enter the class name and click on the OK button. Then, enter the code below in the developer console and save it.
public class StringToDatetime {
public static void convertDateTime() {
String dateString = '2024-09-24 15:30:00';
DateTime convertedDateTime = DateTime.valueOf(dateString);
System.debug('Converted DateTime: ' + convertedDateTime);
}
}- If there are no errors in the above code, execute the anonymous window with “Ctrl+E.“
- Enter the code below in the anonymous window and click the Execute button.
StringToDatetime.convertDateTime();- In the execution log window, select the checkbox to debug only, and there you will see the Datetime value converted from the String.

In this way, we can convert a string to a datetime data type in Salesforce Apex by following the above steps.
Convert String to GMT Datetime in Salesforce Apex
To convert a String to a Datetime in Salesforce Apex, navigate to the developer console and follow the steps below.
- Create a new class in the developer console and create a Class; select File > New > Apex Class.
- Then, enter the name of the class and click OK. After this, enter the code below in the apex class and save it.
public class DatetimeConverter {
public static String convertToGMT(String val) {
DateTime date1 = (DateTime)Json.deserialize('"' + val + '"', DateTime.class);
System.debug('Deserialized DateTime: ' + date1);
String convertedDate = date1.formatGmt('yyyy-MM-dd\'T\'HH:mm:ss\'Z\'');
System.debug('Converted GMT DateTime: ' + convertedDate);
return convertedDate;
}
}Let’s break down the above apex code.
In the above code, we have used two code blocks. The first one, DateTime date1, converts a string to a datetime, and String convertedDate sets the converted datetime according to the GMT zone.
- In the method DateTime date1, “Json.deserialize” converts (deserializes) this string into a DateTime object (DateTime.class), which can then be used for further operations.
- date1.formatGmt(‘yyyy-MM-dd\’T\’HH:mm:ss\’Z\”): It formats the DateTime object date1 into the specified GMT format.
- The format string ‘my-MM-dd\’T\’HH:mm:ss\’Z\” results in a format like 2024-09-24T12:34:56Z,
- If the apex code contains no problems (errors), execute the anonymous window using “Ctrl+E.”
- In the anonymous window, enter the code below and click the Execute button.
String dateTimeString = '2024-09-24T10:00:00.000Z';
String gmtDate = DateTimeConverter.convertToGMT(dateTimeString);
System.debug('Converted GMT Datetime: ' + gmtDate);- In the Execution Log window, select the checkbox to enable debugging. Only then will you see the string converted to a datetime in the GMT zone.

In this way, we can convert a string to a GMT datetime in Salesforce Apex by following the above steps.
Convert a String to a Datetime using the newInstance() method in Salesforce Apex.
In Salesforce Apex, another string-to-datetime conversion method is the newInstance() method. In this method, we must use the date and time string in separate variables, and then combine them in the output value to convert it to a datetime.
This method works if the date and time components are provided separately. If you’re handling a complete date-time string, you’d need to use DateTime.valueOf() instead.
Follow the steps below to convert a string to a datetime in Salesforce Apex using the newInstance() method.
- In the developer console, launch the anonymous execution window with “Ctrl+E.”
- Enter the below code in the anonymous window and enter the below code.
String dateStr = '2024-09-30';
String timeStr = '12:34:56';
Date newDate = Date.valueOf(dateStr);
Time newTime = Time.newInstance(12, 34, 56, 0);
DateTime newDateTime = DateTime.newInstance(newDate, newTime);
System.debug('DateTime: ' + newDateTime);- Date.valueOf() converts the string dateStr to a Date object.
- Time.newInstance() converts the string timeStr to a Time object by specifying the hours, minutes, and seconds.
- DateTime.newInstance() then combines the Date and Time to create a DateTime object.
- In the anonymous window, select the “Open in Debug” checkbox and click the Execute button.
- In the execution log window, select the checkbox to debug only, and then you will see the datetime value converted from a string.

This method converts strings to a datetime data type using the DateTime.newIntance(date,time) method.
Convert String to Datetime using Deserialization in Salesforce Apex
Recently, I was trying to convert a string to a datetime using the parse() method, and I was getting the “invalid datetime format error.” I took a different approach and got the desired output using the deserialize method.
Navigate to the Salesforce developer console and follow the steps below.
- In the Salesforce developer console, launch the anonymous code window with “Ctrl+E”.
- Enter the code below in the anonymous window, and click on the Execute button.
String StringDateTime = '2024-06-24 10:21:44';
String auxDate = StringDateTime.substringBefore(' ');
String auxTime = StringDateTime.substringAfter(' ');
String isoDateTime = auxDate + 'T' + auxTime;
Datetime convertedDateTime = (DateTime)JSON.deserialize('"' + isoDateTime + '"', DateTime.class);
System.debug(' -> CONVERTED DATETIME : ' + convertedDateTime);- String StringDateTime; initializes a string with a date and time.
- The string is split into two parts: auxDate (date) and auxTime (time).
- These parts are combined in ISO format (yyyy-MM-dd’T’HH:mm:ss) and stored in isoDateTime.
- Then isoDateTime is converted into a Datetime type using JSON.deserialize.
- In the execution log window, select the ‘Debug only’ checkbox, and we will see the output as a datetime value converted from a string.

In this way, we can convert a string to a datetime data type using the Json deserialize method by following the above steps.
Conclusion
In this Salesforce tutorial, we have learned about the above possible ways to convert a string to a datetime in Salesforce Apex. In the above methods, we used the valueOf(), newInstance(), and JSON deserialization methods.
By now, you might have understood the method of converting strings to datetime in Salesforce Apex, and with this, you will be able to manage the data in your Salesforce org more efficiently.
You may also like to read.
- Convert ID to String in Salesforce Apex
- Convert a String to Date in Salesforce Apex
- Convert String to ID in Salesforce Apex
- Convert a String to an Integer using Apex in Salesforce
- Convert a String to Decimal in Salesforce Apex
I am Bijay Kumar, the founder of SalesforceFAQs.com. Having over 10 years of experience working in salesforce technologies for clients across the world (Canada, Australia, United States, United Kingdom, New Zealand, etc.). I am a certified salesforce administrator and expert with experience in developing salesforce applications and projects. My goal is to make it easy for people to learn and use salesforce technologies by providing simple and easy-to-understand solutions. Check out the complete profile on About us.