Quick guide: get weekday names from Date and compute actual business-hours time between two Datetime values in Apex (handles Mon-Fri 8:00-20:00, Sat-Sun 8:00-16:30).
Overview
When working with Salesforce, its common to need the weekday name for reporting or to calculate SLAs, and to compute the actual time spent between two DateTime fields while excluding non-working hours. This article shows two compact Apex utilities: one to map a Date to its day name, and another to compute the total hours spent during defined business hours across a datetime range.
Get Day of Week from Date
Salesforce doesn’t expose a direct Date.getDayName() method. Use toStartOfWeek()
and daysBetween()
to compute the day index (0=Sunday) and map it to a string.
public static String getDayOfWeek(Date inputDate) { Date startOfWeek = inputDate.toStartOfWeek(); Integer dayDifference = inputDate.daysBetween(startOfWeek); String dayOfWeek; switch on dayDifference { when 0 { dayOfWeek = 'Sunday'; } when 1 { dayOfWeek = 'Monday'; } when 2 { dayOfWeek = 'Tuesday'; } when 3 { dayOfWeek = 'Wednesday'; } when 4 { dayOfWeek = 'Thursday'; } when 5 { dayOfWeek = 'Friday'; } when 6 { dayOfWeek = 'Saturday'; } when else { dayOfWeek = 'Invalid'; } } return dayOfWeek; }
How it works
toStartOfWeek()
returns the Sunday for the week.daysBetween()
gives the number of days between the input date and that Sunday.- The day index maps to the weekday name.
Calculate Business Hours Between Two DateTimes
This function iterates each date in the range and accumulates only the overlap between the datetime window and configured business hours. Business hours used here:
- Weekdays (Mon-Fri): 08:00 – 20:00
- Weekends (Sat-Sun): 08:00 – 16:30
public static Decimal calculateBusinessHours(Datetime startDateTime, Datetime endDateTime) { if (startDateTime == null || endDateTime == null) { return 0; } Date startDate = startDateTime.date(); Date endDate = endDateTime.date(); Decimal totalHours = 0; for (Date currentDate = startDate; currentDate <= endDate; currentDate = currentDate.addDays(1)) { Date startOfWeek = currentDate.toStartOfWeek(); Integer dayDifference = currentDate.daysBetween(startOfWeek); Datetime businessStart = Datetime.newInstance(currentDate, Time.newInstance(8, 0, 0, 0)); Datetime businessEnd; if (dayDifference == 6 || dayDifference == 0) { // Saturday or Sunday businessEnd = Datetime.newInstance(currentDate, Time.newInstance(16, 30, 0, 0)); } else { businessEnd = Datetime.newInstance(currentDate, Time.newInstance(20, 0, 0, 0)); } Datetime effectiveStart = (startDateTime > businessStart) ? startDateTime : businessStart; Datetime effectiveEnd = (endDateTime < businessEnd) ? endDateTime : businessEnd; if (effectiveStart < effectiveEnd) { totalHours += (effectiveEnd.getTime() - effectiveStart.getTime()) / (1000 * 60 * 60); } } return totalHours; }
Notes & Best Practices
- All calculations use GMT values from Datetime.getTime(); ensure your org timezone implications are considered.
- To support public holidays, supply a Set
of holidays and skip those dates in the loop. - For global orgs with multiple business schedules, parameterize start/end times per day or use Salesforce BusinessHours class where appropriate.
Why this matters
Accurately mapping weekdays and counting business-time elapsed is essential for SLA tracking, case aging, lead response time measurement, and any time-based automations in Salesforce. The provided utilities are lightweight, easy to adapt, and can be extended to include holiday calendars or org-level business hours.
Category: Salesforce Tips
Tags: Apex, BusinessHours, Calculate Time Difference, Day of the Week, Excluding Off Hours from Calculation
Leave a Reply