如何计算工作 15 天并获得结束日期?

用户入住酒店时如何计算我的实际工作日?除了周六和周日,我只想计算工作日。请检查下面的函数它的工作日计数,但在参数中我输入了开始日期和结束日期。我想只发送开始日期,它会自动计算 15 个工作日并返回我的结束日期。//天数


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;

}


撒科打诨
浏览 165回答 2
2回答

慕田峪9158850

它应该是泛型方法。您可以在其他地方添加不同的工作日。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);

犯罪嫌疑人X

这里有两种方法。这个想法是生成范围内的每个日期,决定它是否是工作日,然后才将其添加到结果列表中。GetBusinessDaysInRange返回给定开始日期和结束日期之间的工作日日期列表。结束日期是排他性的,即如果结束日期是工作日,则它不会成为结果的一部分。// Returns a list of the dates of the Business Days between the given start and end datepublic static IEnumerable<DateTime> GetBusinessDaysInRange(DateTime startDate, DateTime endDate, DayOfWeek[] closedOn) {&nbsp; &nbsp;if (endDate < startDate) {&nbsp; &nbsp; &nbsp; &nbsp; throw new ArgumentException("endDate must be before startDate");&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}&nbsp; &nbsp; var businessDays = new List<DateTime>();&nbsp; &nbsp; var date = startDate;&nbsp; &nbsp; while (date < endDate) {&nbsp; &nbsp; &nbsp; &nbsp; if (!closedOn.Contains(date.DayOfWeek)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; businessDays.Add(date);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; date = date.AddDays(1);&nbsp; &nbsp; }&nbsp; &nbsp; return businessDays;}GetFixedNumberOfBusinessDays 从给定的开始以给定的天数(您要求的方法)返回工作日的日期列表。// Returns a list of the dates of the Business Days from the given start with the given number of dayspublic static IEnumerable<DateTime> GetFixedNumberOfBusinessDays(DateTime startDate, int numberOfBusinessDays, DayOfWeek[] closedOn) {&nbsp; &nbsp; if (numberOfBusinessDays < 0) {&nbsp; &nbsp; &nbsp; &nbsp; throw new ArgumentException("numberOfBusinessDays must be zero or positive.");&nbsp;&nbsp;&nbsp; &nbsp; }&nbsp; &nbsp; var businessDays = new List<DateTime>();&nbsp; &nbsp; var date = startDate;&nbsp; &nbsp; while (businessDays.Count() < numberOfBusinessDays) {&nbsp; &nbsp; &nbsp; &nbsp; if (!closedOn.Contains(date.DayOfWeek)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; businessDays.Add(date);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; date = date.AddDays(1);&nbsp; &nbsp; }&nbsp; &nbsp; return businessDays;}DayOfWeek[] closedOn引入该参数是因为您不想对不是工作日的星期几进行硬编码。返回类型已更改为,IEnumerable<DateTime>因此此方法更通用。如果您只想要天数并且对实际日期不感兴趣,只需.Count()对结果运行 a 。如果您想要结束日期,请致电.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&nbsp; &nbsp;&nbsp;DateTime endDate = businessDays.Last();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Friday, August 3, 2018
打开App,查看更多内容
随时随地看视频慕课网APP