BusinessHours bh = [SELECT Id, Name, IsActive, IsDefault, SundayStartTime, SundayEndTime, MondayStartTime, MondayEndTime, TuesdayStartTime, TuesdayEndTime, WednesdayStartTime, WednesdayEndTime, ThursdayStartTime, ThursdayEndTime, FridayStartTime, FridayEndTime, SaturdayStartTime, SaturdayEndTime, TimeZoneSidKey FROM BusinessHours WHERE IsDefault = true]; DateTime startTime; DateTime endTime; Datetime dt = System.now(); String dayOfWeek = dt.format('EEEE'); if(dayOfWeek.equalsIgnoreCase('Saunday')){ startTime = DateTime.newInstance(Date.today(), Time.newInstance(bh.SundayStartTime.hour(), bh.SundayStartTime.minute(), bh.SundayStartTime.second(), 0)); endTime = DateTime.newInstance(Date.today(), Time.newInstance(bh.SundayEndTime.hour(), bh.SundayEndTime.minute(), bh.SundayEndTime.second(), 0)); }else if(dayOfWeek.equalsIgnoreCase('Monday')){ startTime = DateTime.newInstance(Date.today(), Time.newInstance(bh.MondayStartTime.hour(), bh.MondayStartTime.minute(), bh.MondayStartTime.second(), 0)); endTime = DateTime.newInstance(Date.today(), Time.newInstance(bh.MondayEndTime.hour(), bh.MondayEndTime.minute(), bh.MondayEndTime.second(), 0)); }else if(dayOfWeek.equalsIgnoreCase('TuesDay')){ startTime = DateTime.newInstance(Date.today(), Time.newInstance(bh.TuesdayStartTime.hour(), bh.TuesdayStartTime.minute(), bh.TuesdayStartTime.second(), 0)); endTime = DateTime.newInstance(Date.today(), Time.newInstance(bh.TuesdayEndTime.hour(), bh.TuesdayEndTime.minute(), bh.TuesdayEndTime.second(), 0)); }else if(dayOfWeek.equalsIgnoreCase('Wednesday')){ startTime = DateTime.newInstance(Date.today(), Time.newInstance(bh.WednesdayStartTime.hour(), bh.WednesdayStartTime.minute(), bh.WednesdayStartTime.second(), 0)); endTime = DateTime.newInstance(Date.today(), Time.newInstance(bh.WednesdayEndTime.hour(), bh.WednesdayEndTime.minute(), bh.WednesdayEndTime.second(), 0)); }else if(dayOfWeek.equalsIgnoreCase('Thursday')){ startTime = DateTime.newInstance(Date.today(), Time.newInstance(bh.ThursdayStartTime.hour(), bh.ThursdayStartTime.minute(), bh.ThursdayStartTime.second(), 0)); endTime = DateTime.newInstance(Date.today(), Time.newInstance(bh.ThursdayEndTime.hour(), bh.ThursdayEndTime.minute(), bh.ThursdayEndTime.second(), 0)); }else if(dayOfWeek.equalsIgnoreCase('Friday')){ startTime = DateTime.newInstance(Date.today(), Time.newInstance(bh.FridayStartTime.hour(), bh.FridayStartTime.minute(), bh.FridayStartTime.second(), 0)); endTime = DateTime.newInstance(Date.today(), Time.newInstance(bh.FridayEndTime.hour(), bh.FridayEndTime.minute(), bh.FridayEndTime.second(), 0)); }else if(dayOfWeek.equalsIgnoreCase('Saturday')){ startTime = DateTime.newInstance(Date.today(), Time.newInstance(bh.SaturdayStartTime.hour(), bh.SaturdayStartTime.minute(), bh.SaturdayStartTime.second(), 0)); endTime = DateTime.newInstance(Date.today(), Time.newInstance(bh.SaturdayEndTime.hour(), bh.SaturdayEndTime.minute(), bh.SaturdayEndTime.second(), 0)); } //get the difference between start time and end time Long difference = BusinessHours.diff(bh.id, starttime, endtime); Double hours = difference/3600000; System.debug('Business Hours Diff-' + hours);
Get Weekly Days Business Hours Difference
Biswajeet
November 9, 2022 No Comments on Get Weekly Days Business Hours Difference