How Can I Tell the Day of the Week of a Date?

Determining the day of the week from a given date is a common requirement when building logic in Salesforce. You might be asking, “How can I tell the day of the week of a date?” You can achieve this using Apex code or Salesforce formulas. Here’s how to approach this problem effectively.
Using Apex Code to Find the Day of the Week
One efficient way to calculate the day of the week is by using the daysBetween()
function in Apex. By choosing a reference date, such as January 1, 1900, which is a Monday, you can calculate the day of the week for any given date. The result can then be indexed where Monday = 0, Tuesday = 1, and so on.
Elevate your career with Salesforce Online Training, offering expert instruction, hands-on learning, and certification support. Empower your future with confidence!
Here’s an example:
Date monday = Date.newInstance(1900, 1, 1); // Reference Date: Monday, Jan 1, 1900
Date dateValue = Date.newInstance(2025, 1, 16); // Example date: Jan 16, 2025
// Calculate the day of the week index
Integer dayOfWeekIndex = Math.mod(monday.daysBetween(dateValue), 7);
// Output the index (Monday = 0, Sunday = 6)
System.debug('Day of the Week Index: ' + dayOfWeekIndex);
// Example mapping (you can customize)
String[] dayNames = new String[] { 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday' };
System.debug('Day of the Week: ' + dayNames[dayOfWeekIndex]);
See also: How to use Salesforce Einstein AI in Sales Cloud?
How It Works:
daysBetween(dateValue)
calculates the total number of days between the given date and the reference date.Math.mod()
ensures the result falls between 0-6, representing the days of the week.- Optionally, you can map the index to day names for better readability.
For example:
- For January 16, 2025, the index is
4
, which corresponds to Thursday. - For January 7, 1900, the index is
6
, which corresponds to Sunday.
Example with Edge Cases: Handling Dates Before the Reference Date
The above method works well for dates after January 1, 1900, but you may encounter unexpected results for earlier dates. Let’s test it:
Date monday = Date.newInstance(1900, 1, 1); // Reference Date
Date pastDate = Date.newInstance(1899, 12, 28); // Example: Dec 28, 1899
// Calculate the day of the week
Integer pastDayOfWeekIndex = Math.mod(monday.daysBetween(pastDate), 7);
// Debug result
System.debug('Past Date Day of the Week Index: ' + pastDayOfWeekIndex);
For December 28, 1899, this may not yield the correct index due to limitations in date handling. If your use case involves earlier dates, consider other approaches or account for the offset.
See also: How to Retrieve and Operate on JSON in LWC?
Alternative: Using Date.format()
for Locale-Specific Day Names
If you prefer to work with day names directly, you can use the format()
method. However, keep in mind that the output may vary depending on the user’s locale settings.
Date dateValue = Date.newInstance(2025, 1, 16); // Example date
String dayName = dateValue.format('EEEE'); // Returns the full day name (e.g., "Thursday")
System.debug('Day of the Week: ' + dayName);
Limitations:
- This method relies on locale-specific formatting.
- It’s useful when you need the name of the day but may not be ideal for numeric calculations.
Using Formulas to Calculate the Day of the Week
You can also calculate the day of the week using a formula field. Salesforce provides a MOD
function, similar to Math.mod()
in Apex. Here’s a formula example:
MOD(DateValue - DATE(1900, 1, 1), 7)
Replace DateValue
with your date field. This formula calculates the day of the week based on the difference in days from January 1, 1900. The result will be a number between 0 and 6, which you can map to specific days.
See also: How Can I Troubleshoot DataKit Connect API Deployment Issues?
Conclusion
There are multiple ways to determine the day of the week for a given date in Salesforce:
- Use the
daysBetween()
function with a reference date in Apex for reliable and locale-independent results. - Use the
Date.format()
method if you prefer to work with day names directly. - Implement formulas for declarative use cases.
The best method depends on your specific requirements and whether you need to calculate day indices or display day names. For robust solutions, Apex is often the most reliable choice.
Why Opt for Salesforce Online Training?
CRS Info Solutions offers a comprehensive Salesforce Online Training program designed to guide beginners through every stage of their learning path. This course emphasizes hands-on practice, blending real-world applications with a solid conceptual foundation. From interactive video lessons and daily notes to targeted interview preparation, CRS ensures you gain the expertise and confidence needed to achieve Salesforce certifications and boost your career.
Known for its leadership in Salesforce education, CRS Info Solutions provides in-depth training across modules like Admin, Developer, Integration, and Lightning Web Components (LWC). Their seasoned instructors integrate industry-relevant insights with practical learning to equip you with skills that meet market demands. Take the leap toward success today! Start your Salesforce journey with a free demo and unlock your true potential in the Salesforce ecosystem..!!