How to Convert Date to String in Salesforce Apex

While working as a Salesforce developer, I created a report that displayed the last activity date for each account. The LastActivityDate field in Salesforce is a Date type, but I wanted to display it as a formatted string, such as “October 1, 2024” in a custom email notification.

To achieve this, I converted the Date to a string using Apex’s format() method, making the report and email more readable.

In this Salesforce tutorial, I will explain to you how to convert a date to a string in Salesforce Apex.

Convert Current Date to String in Salesforce Apex

To convert the Date type to a string in Apex, we will use the date.format() method, and for that, follow the steps below.

  1. In the Salesforce developer console, launch the anonymous code window and enter the code below.
Date myDate = Date.today();
String dateString = myDate.format();
System.debug('Formatted Date String: ' + dateString);
  1. In the above code, the Date.today() will take the current date, and then myDate.format() will convert the date type to display as a string.
    • In the execution log window, select the checkbox to debug only. The string converted from the date type will be displayed in the output.
Convert date to string in Salesforce Apex

This way, we can convert a date type to a string in Salesforce Apex using the date.format() method.

Convert Specific Date to String in Salesforce Apex

To convert a specific date to a string in Salesforce Apex, we can use the date.format() method, but we have to enter the date value in the ‘yyyy-MM-dd‘ format for a specific date.

Now, open the Salesforce developer console and follow the steps below.

  1. In the Salesforce developer console, enter the code below and click the Execute button.
Date specificDate = Date.newInstance(2024, 12, 25); 
String dateString = specificDate.format();
System.debug('Formatted Date String: ' + dateString);
  1. In the execution log window, the debug will show the output as a date type converted to a string in ‘dd-MM-yyyy‘ format.
Salesforce apex convert date to string in dd-mm-yyyy format

This way, we can convert a specific date to a string type in Salesforce Apex by following the above steps.

Salesforce Apex Convert Current Date to String in ‘MM-dd-yyyy’ format

In Apex, we can convert a date-to-date string in ‘MM-dd-yyyy‘ format.

Recently, when I attempted to convert the date to a date string in ‘MM-dd-yyyy‘ format using the date.format(‘MM-dd-yyyy’) method, I got the error messageMethod does not exist or incorrect signature.”

Then, I found an alternate way to convert a date-to-date string in ‘MM-dd-yyyy‘ format, which I have explained in the steps below.

  1. Launch the anonymous window in the Salesforce developer console. Then, enter the code below and click the Execute button.
Date currentDate = Date.today(); 
String formattedDate = String.valueOf(currentDate.month()) + '-' + 
                       String.valueOf(currentDate.day()) + '-' + 
                       String.valueOf(currentDate.year());

System.debug(formattedDate);

In the above code, Date.today() will get the current date. Then, in formattedDate, we used the .month().day(), and .year() to fetch and add the values of month, day, and year.

  1. In the execution log window, select the checkbox to debug only. The converted date will be displayed in the output as a string in ‘MM-dd-yyyy‘ format.
Salesforce apex date to string conversion in 'MM-dd-yyyy' format

This way, we can convert the Current Date to a String in ‘MM-dd-yyyy‘ format in Salesforce Apex.

Convert date to string format ‘yyyy-mm-dd’ in Salesforce Apex

In Salesforce Apex, if you want to format a Date to yyyy-MM-dd specifically, you can convert the Date to a DateTime and then format it.

Now, follow the steps below to convert the date to the string format ‘yyyy-mm-dd’ in Salesforce Apex.

1. In the Salesforce developer console, launch the anonymous Apex window. After that, enter the code below and click the Execute button.

Date myDate = Date.today();
DateTime myDateTime = DateTime.newInstance(myDate, Time.newInstance(0, 0, 0, 0)); 
String formattedDate = myDateTime.format('yyyy-MM-dd'); 
System.debug(formattedDate); 

2. In the execution log window, the output will be displayed as a date string in ‘yyyy-mm-dd‘ format.

Salesforce convert date string in 'yyyy-mm-dd' format

In this way, we can convert a date type to a string in the format of ‘yyyy-mm-dd‘ in Salesforce Apex.

Conclusion

In this Salesforce tutorial, we have learned to convert date type to string in Apex. By using the date.format() method, we converted current and specific date values to strings. After this, we also discussed the date-to-string conversion for specific date formats.

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.