Extract Year, Month, and Day from a Date Field in Salesforce Apex

While working as a Salesforce developer, I was assigned a task to build a custom logic that breaks down a date field into its individual components—year, month, and day. This was required so that we could generate customized reports and perform date-based filtering for opportunities.

To solve this, I had to extract the year, month, and day from the custom CloseDate__c field on the Opportunity object.

In this Salesforce tutorial, I will explain how to extract Year, Month, and Day from a Date Filed in Salesforce Apex.

Extract Year, Month, and Day from a Date Field in Salesforce Apex

Salesforce Apex has various approaches and methods to extract and get the Year, Month, and Day from a Date field in Salesforce Apex, which we will discuss in the steps below.

Extract Year, Month, and Day from a Date Field using the Date Class

To get Year, Month, and Day from a Date Field using the Date class, open the Salesforce developer, enter the code below in the Apex anonymous window, and execute it.

Date todayDate = Date.today();

Integer year = todayDate.year();
Integer month = todayDate.month();
Integer day = todayDate.day();

System.debug('Year: ' + year);
System.debug('Month: ' + month);
System.debug('Day: ' + day);

In the execution log window, we will use the date(day), month, and year values extracted from today’s date.

Extract day date and year from date in Salesforce Apex

Instead of the today() date, we can also use a specific date to extract the day, month, and year values using the Date class method.

Extract Year, Month, and Day from a Datetime field

In Salesforce, we can use the Datetime class to get the Year, Month, and Date from the Datetime fields.

DateTime dateTimeValue = DateTime.now();

Integer year = dateTimeValue.year();
Integer month = dateTimeValue.month();
Integer day = dateTimeValue.day();

System.debug('Date Year: ' + year);
System.debug('Date Month: ' + month);
System.debug('Date Day: ' + day);

In the execution log window, we can see the values of extracted day, month, and year from the datetime field.

Extract day date and year from datetime in Salesforce Apex

In this way, we can extract the values of day, month, and year from the datetime field in Salesforce Apex using the dateTimeValue() method.

Extract Year, Month, and Day from the Date field Using Date.format() Method for a Custom format

When you have to extract the day, date, and year from the date field in a specific format, such as the “dd/MM/yyyy” format instead of “yyyy/MM/dd.”

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

Date todayDate = Date.today();

String formattedDay = String.valueOf(todayDate.day());
String formattedMonth = String.valueOf(todayDate.month());
String formattedYear = String.valueOf(todayDate.year());

System.debug('Day: ' + formattedDay);
System.debug('Month: ' + formattedMonth);
System.debug('Year: ' + formattedYear);

After executing the above code, the debug will return the output of the extracted day, month, and year in the entered format “dd/MM/YYYY.”

Salesforce apex extract day month and year

This way, we can extract Year, Month, and Day from the Date field using the  Date.format() method for custom formatting.

Conclusion

In this Salesforce tutorial, we learned to extract the values of year, month, and day from a specific date field. We discussed the various methods, such as date class, datetime class, and date.format(), through which we have successfully extracted the values of day, month, and year from a date field.

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.