Convert DateTime to Date Format in Salesforce Apex

Our Salesforce org has an app event management system storing events with start and end times (Datetime fields). However, the user needs to view all events that occur on a particular date, regardless of when the event starts or ends. To implement this, we need to filter the Datetime field by converting it to Date.

To solve this, I converted the datetime value to the date format using Apex in Salesforce. In this Salesforce tutorial, I will explain how to convert datetime to Date Format in Salesforce Apex using various methods.

Convert DateTime to a Date format in Salesforce Apex

The steps below show the various methods to convert the datetime to a date format in Salesforce Apex.

Convert DateTime to a Date format using the newInstance() method in Salesforce Apex

To convert DateTime to a Date format using the newInstance() method in Salesforce Apex, navigate to the Salesforce developer console, enter the code below in the Apex anonymous window, and execute it.

DateTime dt = Datetime.now();
Date d = Date.newInstance(dt.year(), dt.month(), dt.day());
System.debug('Converted Datetime to date: ' + d);

Now, in the execution log’s debug window, we will see that the Datetime is converted to the date format in Apex.

Convert Datetime to Date in Salesforce Apex

This way, we can convert a DateTime value to Date format in Salesforce Apex using the newInstance() method.

Convert DateTime to a Date format using the Date() method in Salesforce Apex

In Salesforce Apex, another method to get the Date value from Datetime is the Date() method.

Now, open the Salesforce developer console and execute the code below in the Apex anonymous window.

DateTime dt = Datetime.now();
Date d = dt.date();
System.debug('Converted Datetime to date: ' + d);

After executing the above apex code, the datetime value will be converted to the date format in Salesforce Apex.

Convert datetime to date in Salesforce Apex using Date method

This way, we can convert a DateTime value to Date format in Salesforce Apex using the Date() method.

Convert DateTime to a Date format using the format() method in Salesforce Apex

To convert a DateTime value to a Date format using the format() method in Salesforce Apex, open the Salesforce developer console and execute the code below in the Apex anonymous window.

Datetime dt = Datetime.now();
Date dateOnly = dt.date();
System.debug('formated Date value :  ' + dateOnly);

In the Apex execution log window, we see that the datetime value is converted to date format in the debug output.

Convert datetime to date in Salesforce Apex using format method

Using the format() method according to the above code structure, we can convert the datetime value to a date in Salesforce Apex.

Convert DateTime to a Date format using the Parse() method in Salesforce Apex

In Salesforce Apex, we can parse the DateTime to a String and then parse the string to a Date value.

To convert a datetime to a date using the parse method, open the Salesforce developer console and execute the code below in the Apex anonymous window.

Datetime dt = Datetime.now();
String dateString = dt.format('yyyy-MM-dd');
Date parsedDate = Date.valueOf(dateString);
System.debug('Parsed Date value : ' + parsedDate);

Now, the datetime value will be converted into date format, and we can see that in the execution log window’s debug.

Convert datetime to date using parse method in Salesforce Apex

This way, we can convert the datetime value to a date in Salesforce Apex using the parse method.

Conclusion

In this Salesforce tutorial, we have learned to convert the datetime to a date value in Salesforce Apex. We have discussed the various methods to convert the datetime value to date format, such as Date.newInstance(), Date(), format(), and Parse() methods.

Using the above methods, you can efficiently convert the datetime value to the date format in your Salesforce Apex environment.

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.