如何计算15天的工作时间并得出终止日期?

时间:2018-07-23 09:13:33

标签: c# asp.net

如何计算用户在酒店签到时的实际工作日?我只想计算周六和周日除外的工作日。请在下面的功能中检查其工作日,但在参数I中输入了开始日期和结束日期。 我只想发送startdate,它会自动计数15个工作日,然后返回给我enddate。 //天数

public static double GetBusinessDays(DateTime startD, DateTime endD)
{
    double calcBusinessDays =
           1 + ((endD - startD).TotalDays * 5 -
           (startD.DayOfWeek - endD.DayOfWeek) * 2) / 7;

    if (endD.DayOfWeek == DayOfWeek.Saturday) calcBusinessDays--;
    if (startD.DayOfWeek == DayOfWeek.Sunday) calcBusinessDays--;

    return calcBusinessDays;
} 

我想要这样:

public static Datetime GetBusinessDays(DateTime startDate)
{
    Datetime After15WorkingDaysDate;
    return After15WorkingDaysDate;
}

2 个答案:

答案 0 :(得分:1)

这是两种方法。

想法是生成范围内的每个日期,确定是否为工作日,然后将其添加到结果列表中。

GetBusinessDaysInRange返回给定开始日期和结束日期之间的工作日日期列表。结束日期是排他性的,即,如果结束日期是工作日,则不会成为结果的一部分。

// Returns a list of the dates of the Business Days between the given start and end date
public static IEnumerable<DateTime> GetBusinessDaysInRange(DateTime startDate, DateTime endDate, DayOfWeek[] closedOn) {

   if (endDate < startDate) {
        throw new ArgumentException("endDate must be before startDate");    
   }

    var businessDays = new List<DateTime>();
    var date = startDate;

    while (date < endDate) {
        if (!closedOn.Contains(date.DayOfWeek)) {
            businessDays.Add(date);
        }
        date = date.AddDays(1);
    }

    return businessDays;
}

GetFixedNumberOfBusinessDays返回从给定开始到给定天数(您要求的方法)的营业日的列表。

// Returns a list of the dates of the Business Days from the given start with the given number of days
public static IEnumerable<DateTime> GetFixedNumberOfBusinessDays(DateTime startDate, int numberOfBusinessDays, DayOfWeek[] closedOn) {

    if (numberOfBusinessDays < 0) {
        throw new ArgumentException("numberOfBusinessDays must be zero or positive.");  
    }

    var businessDays = new List<DateTime>();
    var date = startDate;

    while (businessDays.Count() < numberOfBusinessDays) {
        if (!closedOn.Contains(date.DayOfWeek)) {
            businessDays.Add(date);
        }
        date = date.AddDays(1);
    }

    return businessDays;
}

引入参数DayOfWeek[] closedOn的原因是,您不想对非工作日的星期几进行硬编码。

返回类型已更改为IEnumerable<DateTime>,因此此方法更为通用。如果您只需要天数,而不对实际日期感兴趣,只需对结果运行.Count()。如果需要结束日期,请致电.Last()

.Net Fiddle以及用法示例:

var closedOn = new DayOfWeek[] { DayOfWeek.Saturday, DayOfWeek.Sunday };
var start = new DateTime(2018, 07, 23);
var numberOfDays = 10;

var businessDays = GetFixedNumberOfBusinessDays(end, numberOfDays, closedOn);

int actualNumberOfBusinessDays = businessDays.Count(); // 10    
DateTime endDate = businessDays.Last();                // Friday, August 3, 2018

答案 1 :(得分:1)

它应该是通用方法。您可以在其他地方添加其他工作日。

public static DateTime AddWorkdays(this DateTime originalDate, int workDays)
        {
            DateTime tmpDate = originalDate;
            while (workDays > 0)
            {
                tmpDate = tmpDate.AddDays(1);
                if (tmpDate.DayOfWeek == DayOfWeek.Saturday ||  
                    tmpDate.DayOfWeek == DayOfWeek.Sunday )
                    workDays--;
            }
            return tmpDate;
        }

DateTime endDate = startDate.AddWorkdays(15);