patterncsharpMinor
Calculate next working day
Viewed 0 times
dayworkingcalculatenext
Problem
I have tried to tackle this question as follows:
```
public static DateTime getNextWorkingDay(DateTime OrderDate, int workingDays)
{
DateTime Holiday = new DateTime();
List SelectedHolidays = new List();
List HolidayList = new List();
int calculatedWorkingDays=0;
if (workingDays OrderDate.Date)
{
int K = (int)(Holiday.Date - OrderDate.Date).TotalDays;
if (K <= calculatedWorkingDays)
{
calculatedWorkingDays++;
}
}
}
}
if (OrderDate.DayOfWeek == DayOfWeek.Sunday)
{
calculatedWorkingDays++;
}
else if (OrderDate.DayOfWeek == DayOfWeek.Saturday || OrderDate.DayOfWeek == DayOfWeek.Friday)
{
calculatedWorkingDays += 2;
}
// this is required date
DateTime date_OrderRequestedToShip = OrderDate.AddDays(calculatedWorkingDays);
//check if the last date fall on a non working day(holiday or weekend)
calculatedWorkingDays = 0;
foreach (DateTime d in SelectedHolidays)
if (d.Date == date_OrderRequestedToShip.Date)
calculatedWorkingDays++;
date_OrderRequestedToShip = date_OrderRequestedToShip.AddDays(calculatedWorkingDays);
if (date_OrderRequestedToShip.DayOfWeek == DayOfWeek.Sunday)
{
calculatedWorkingDays = 1;
}
else if (date_OrderRequestedToShip.DayOfWeek == DayOfWeek.Saturday)
{
calculatedWorkingDays = 2;
The program Calculates the next Working day, taking into account:
- weekends (Saturdays and Sundays)
- holidays happening in the middle of the week
param name="OrderDate" the date on which the order is placed
param name="workingDays" the number of workingdays to process the order
returns the date on which the Order is Requested to Ship```
public static DateTime getNextWorkingDay(DateTime OrderDate, int workingDays)
{
DateTime Holiday = new DateTime();
List SelectedHolidays = new List();
List HolidayList = new List();
int calculatedWorkingDays=0;
if (workingDays OrderDate.Date)
{
int K = (int)(Holiday.Date - OrderDate.Date).TotalDays;
if (K <= calculatedWorkingDays)
{
calculatedWorkingDays++;
}
}
}
}
if (OrderDate.DayOfWeek == DayOfWeek.Sunday)
{
calculatedWorkingDays++;
}
else if (OrderDate.DayOfWeek == DayOfWeek.Saturday || OrderDate.DayOfWeek == DayOfWeek.Friday)
{
calculatedWorkingDays += 2;
}
// this is required date
DateTime date_OrderRequestedToShip = OrderDate.AddDays(calculatedWorkingDays);
//check if the last date fall on a non working day(holiday or weekend)
calculatedWorkingDays = 0;
foreach (DateTime d in SelectedHolidays)
if (d.Date == date_OrderRequestedToShip.Date)
calculatedWorkingDays++;
date_OrderRequestedToShip = date_OrderRequestedToShip.AddDays(calculatedWorkingDays);
if (date_OrderRequestedToShip.DayOfWeek == DayOfWeek.Sunday)
{
calculatedWorkingDays = 1;
}
else if (date_OrderRequestedToShip.DayOfWeek == DayOfWeek.Saturday)
{
calculatedWorkingDays = 2;
Solution
First, you should separate a process of parsing your input from your business logic. So, your first step is to convert your holidays input to a structure you'll be working with. So, let's say you have a
Now, in your place I'd start with implementation of something that enumerates working days like so:
Note that this iterator is endless, so you should be cautious enumerating it.
Now, your problem can be solved with just one line
Complete example here: https://dotnetfiddle.net/W3dalr
List wherepublic struct Holiday
{
public readonly int Month;
public readonly int Day;
public Holiday(int month, int day)
{
Month = month;
Day = day;
}
}Now, in your place I'd start with implementation of something that enumerates working days like so:
static IEnumerable GetWorkingDays(DateTime startDate, List holidays)
{
var date = startDate;
for (;;date = date.AddDays(1))
{
if (date.DayOfWeek != DayOfWeek.Saturday &&
date.DayOfWeek != DayOfWeek.Sunday &&
holidays.All(holiday => holiday.Day != date.Day ||
holiday.Month != date.Month))
{
yield return date;
}
}
}Note that this iterator is endless, so you should be cautious enumerating it.
Now, your problem can be solved with just one line
var result = GetWorkingDays(orderDate, holidays).Skip(workingDays).First();Complete example here: https://dotnetfiddle.net/W3dalr
Code Snippets
public struct Holiday
{
public readonly int Month;
public readonly int Day;
public Holiday(int month, int day)
{
Month = month;
Day = day;
}
}static IEnumerable<DateTime> GetWorkingDays(DateTime startDate, List<Holiday> holidays)
{
var date = startDate;
for (;;date = date.AddDays(1))
{
if (date.DayOfWeek != DayOfWeek.Saturday &&
date.DayOfWeek != DayOfWeek.Sunday &&
holidays.All(holiday => holiday.Day != date.Day ||
holiday.Month != date.Month))
{
yield return date;
}
}
}var result = GetWorkingDays(orderDate, holidays).Skip(workingDays).First();Context
StackExchange Code Review Q#83668, answer score: 7
Revisions (0)
No revisions yet.