How to Calculate the Day of the Week from a Date and Actual Time Spent Between Two Date-Times in Apex
When working with Salesforce, there are instances where we need to compute the day of the week from a given date or determine the actual time spent between two date-time values, considering only business hours. For example, if we need to track the exact amount of time a user spent on a Lead, Case, or Task, simply subtracting two date-time values won't suffice if we need to exclude non-working hours. In this post, we will cover: How to determine the day of the week from a given date in Apex. How to calculate actual time spent between two date-times, while considering business hours (Monday to Friday: 8 AM - 8 PM, Saturday & Sunday: 8 AM - 4:30 PM). 1. How to Determine the Day of the Week from a Date in Apex Salesforce does not provide a direct method to get the day name (Monday, Tuesday, etc.) from a Date field. However, we can achieve this using toStartOfWeek() and daysBetween() methods . public static String getDayOfWeek ( Date inputDate ) { // Get the start...