0

I have a situation where user define the weekday for delivery and its transit day. When the estimate to arrival(ETA) date is given, I need to calculate the estimate to delivery (ETD) date base on the transit day.

Example:

ETA is 17/Jul/2013, Transit Day: 10 Days, Delivery Day: Tue / Fri

By using AddDays(-10), the ETD would be 8/Jul/2013 (Mon). Due to delivery day is not drop on Tue or Fri, system need to look backward the calendar.

As result, system must take the 5/Jul (Fri) as ETD date.

What is the best practice to archive this request?

4

2 回答 2

2
public DateTime GetLastPosibleDate(DateTime datetime, List<DayOfWeek> days)
{
    DateTime dt = datetime;
    if (!days.Any(d=> d==dt.DayOfWeek))
    {
        dt=  GetLastPosibleDate(datetime.AddDays(-1), days);
    }
    return dt;
}

public DateTime EstimatedDiliveryDate(DateTime arrival, int transitDays, List<DayOfWeek> deliveryDays)
{
    return GetLastPosibleDate(arrival.AddDays(-transitDays), deliveryDays);
}

用法 :

var arrival = new DateTime(2013,07,17);
var deliveryDays = new List<DayOfWeek>(){DayOfWeek.Tuesday, DayOfWeek.Friday};
var result = EstimatedDiliveryDate(arrival, 10, deliveryDays);
于 2013-07-10T04:41:53.187 回答
0

您可以做的是在几天之间进行迭代,直到根据用户的偏好找到下一个交货日。你可以在这里看到一个例子:Get Next Nth Fridays Date From Todays Date

于 2013-07-10T04:12:42.487 回答