0

Basically I have 3 products on my online store. The specifics of each are stored in a List aray. However

  • product 1 ships in 3 working days
  • Product 2 ships the very next working day
  • Product 3 takes 7 working days to ship

How would I calculate the Estimated date of arrival ETDA (excluding saturday and sunday) to find out when the product arrives.

Example : Customer orders product 1, The day when order is placed is Thursday, so product 1 takes 3 working days to ship, in other words it will arrive on Tuesday. How would I perform this in c# showing the user it will be arriving on Tuesday.

Shaharyar
  • 12,254
  • 4
  • 46
  • 66
Chris
  • 47
  • 2
  • 2
  • 6
  • What data store are you using ? SQL, xml, hard coded? – pingoo Aug 22 '13 at 15:21
  • it is hard coded currently – Chris Aug 22 '13 at 15:22
  • 1
    If product 1 takes 3 working days to ship, and the customer orders it on a Thursday, then it should SHIP on Tuesday. When it would arrive is a whole other story. – andi Aug 22 '13 at 15:25
  • 1
    getting business days has a nice implementation here: http://stackoverflow.com/questions/1044688/add-business-days-and-getbusinessdays – Venkata Krishna Aug 22 '13 at 15:25
  • Why not just give the user the tracking code from the shipping agent? I don't think your company can guarantee what happens to a package once it leaves, right? – NWard Aug 22 '13 at 15:26

2 Answers2

3

I would be tempted to do a brute force approach, just because it saves on brain power:

int workingDays = 3;//get from item
DateTime dt = DateTime.Today();
while(workingDays > 0)
{
    dt = dt.AddDays(1);
    if(dt.DayOfWeek != DayOfWeek.Saturday && dt.DayOfWeek != DayOfWeek.Sunday)
        workingDays--;
}
//dt is now the correct day
musefan
  • 47,875
  • 21
  • 135
  • 185
  • A google search saves on brain power: http://stackoverflow.com/questions/1044688/add-business-days-and-getbusinessdays – Ahmed KRAIEM Aug 22 '13 at 15:27
  • 1
    @AhmedKRAIEM: true, but I still prefer mine. It's much shorter, and the performance is going to be minor. Unless you start doing huge amounts of business days, but that would be silly – musefan Aug 22 '13 at 15:29
  • mine is also probably better suited to allow dynamically determining what days are classed as business days – musefan Aug 22 '13 at 15:31
1

You can use this method:

public DateTime DayOfArrival(DateTime startDate, int numberOfWordkingDays )
{
    var result = startDate;
    var counter = 0;
    while (counter < numberOfWordkingDays)
    {
        if (result.DayOfWeek != DayOfWeek.Saturday && result.DayOfWeek != DayOfWeek.Sunday)
            counter++;
        result = result.AddDays(1);
    }
    return result;
}
Alex Siepman
  • 2,499
  • 23
  • 31