public static DateTimeOffset AddBusinessDays(this DateTimeOffset date, int day, BussinessDayConfig config) { if (config == null) { config = new BussinessDayConfig(); } var currentDate = date; var sign = Math.Sign(day); var unsignDays = Math.Abs(day); var workingDay = config.WorkingDay(); var holiday = config.Holiday.Select(x => x.Date).ToList(); while (unsignDays > 0) { currentDate = currentDate.AddDays(sign); if (workingDay.Any(x => x == currentDate.DayOfWeek) && !holiday.Any(c => c == currentDate.Date)) /* if working day. */ { unsignDays -= 1; } } return(currentDate); }
public static DateTime AddWorkingHours(this DateTime dateTime, TimeSpan time, TimeSpan workingHourStart, TimeSpan workingHourEnd, bool sameDay = true, BussinessDayConfig bussinessDayConfig = null) { var startDate = dateTime.Date + workingHourStart; var endDate = dateTime.Date + workingHourEnd; if (dateTime > endDate || (dateTime + time > endDate && sameDay)) { startDate = startDate.AddBusinessDays(1, bussinessDayConfig); endDate = endDate.AddBusinessDays(1, bussinessDayConfig); dateTime = startDate; } var date = dateTime + time; if (date > endDate) { var diff = date - endDate; var remain = time - diff; return(startDate.AddBusinessDays(1, bussinessDayConfig).AddWorkingHours(diff, workingHourStart, workingHourEnd)); } else { return(date); } }
public static DateTime AddWorkingHours(this DateTime dateTime, string time, string workingHourStart = "9:00", string workingHourEnd = "18:00", bool sameDay = true, BussinessDayConfig bussinessDayConfig = null) { var t = TimeSpan.Parse(time); var start = TimeSpan.Parse(workingHourStart); var end = TimeSpan.Parse(workingHourEnd); return(dateTime.AddWorkingHours(t, start, end, sameDay, bussinessDayConfig)); }
public static DateTimeOffset?AddBusinessDays(this DateTimeOffset?date, int day, BussinessDayConfig config) { if (date.HasValue) { return(date.Value.AddBusinessDays(day, config)); } else { return(date); } }