我需要确定几天的时间来确定周末是否在此范围内。如果是这样,我将2天添加到最终范围(周一至周日,将其更改为周一至周三)。 (基于用户选择的天数,例如5天)
我已经提出了一种解析日期的方法(不确定这是否是最佳实践)并且不确定如何继续。到目前为止:
////TODO Refactor to new class
DateTime startingDate = DateTime.Parse("07/25/2018");//TODO Insert variable from poted form
DateTime endingDate = DateTime.Parse("07/08/2018");//TODO Insert variable from posted form
var days= Enumerable.Range(0, 1 + endingDate.Subtract(startingDate).Days)
.Select(offset => startingDate.AddDays(offset))
.ToList();
我认为,我需要像下面的伪代码一样进行操作:
if day.contains dayOfWeek.Saturday ||Or dayofWeek.Friday {Add.Days(2)}
但是我不知道该怎么做。
答案 0 :(得分:2)
用户提供结束日期和工作日数,这将产生一个包含所需金额的列表
DateTime endingDate = DateTime.ParseExact("07/25/2018","MM/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture);//TODO Insert variable from poted form
int workingDays = 5;
List<DateTime> dates = new List<DateTime>();
DateTime startingDate = endingDate;
while(dates.Count<workingDays)
{
if(startingDate.DayOfWeek != DayOfWeek.Sunday && startingDate.DayOfWeek != DayOfWeek.Saturday)
{
dates.Insert(0, startingDate);
}
startingDate = startingDate.AddDays(-1);
}
输出将为
2018-07-19
2018-07-20
2018-07-23
2018-07-24
2018-07-25
20个工作日内:
01 2018-06-28 Thursday
02 2018-06-29 Friday
03 2018-07-02 Monday
04 2018-07-03 Tuesday
05 2018-07-04 Wednesday
06 2018-07-05 Thursday
07 2018-07-06 Friday
08 2018-07-09 Monday
09 2018-07-10 Tuesday
10 2018-07-11 Wednesday
11 2018-07-12 Thursday
12 2018-07-13 Friday
13 2018-07-16 Monday
14 2018-07-17 Tuesday
15 2018-07-18 Wednesday
16 2018-07-19 Thursday
17 2018-07-20 Friday
18 2018-07-23 Monday
19 2018-07-24 Tuesday
20 2018-07-25 Wednesday
答案 1 :(得分:1)
首先计算整个周末。然后,将结果添加到开始日期。这是我的解决方案。希望对我有帮助!
static IEnumerable<DateTime> GetDaysBetween(DateTime start, DateTime end)
{
for (DateTime i = start; i < end; i = i.AddDays(1))
{
yield return i;
}
}
DateTime startingDate = DateTime.Parse("07/25/2018");//TODO Insert variable from poted form
var numOfWeekends = GetDaysBetween(startingDate, startingDate.AddDays(7))
.Where(d => d.DayOfWeek == DayOfWeek.Saturday || d.DayOfWeek == DayOfWeek.Sunday).Count();
startingDate = startingDate.AddDays(numOfWeekends);
Console.WriteLine(startingDate);
答案 2 :(得分:1)
我不太了解addintion部分,但是这段代码将为您提供一个没有任何星期日或星期六的列表:
DateTime startingDate = DateTime.Parse("07/25/2018");//TODO Insert variable from poted form
DateTime endingDate = DateTime.Parse("07/08/2018");//TODO Insert variable from posted form
List<DateTime> dates = new List<DateTime>();
for (DateTime dt = startingDate; dt <= endingDate; dt = dt.AddDays(1))
if (dt.DayOfWeek != DayOfWeek.Saturday && dt.DayOfWeek != DayOfWeek.Sunday)
dates.Add(dt);
如果需要,您还可以添加一个计数器:
DateTime startingDate = DateTime.Parse("07/25/2018");//TODO Insert variable from poted form
DateTime endingDate = DateTime.Parse("07/08/2018");//TODO Insert variable from posted form
List<DateTime> dates = new List<DateTime>();
int couter = 0;
for (DateTime dt = startingDate; dt <= endingDate; dt = dt.AddDays(1))
{
if (dt.DayOfWeek != DayOfWeek.Saturday && dt.DayOfWeek != DayOfWeek.Sunday)
dates.Add(dt);
else
counter++;
}
还有其他什么...