如何从范围内获取缺失的日期
我正在做这个测试应用程序,它要做的就是从文本文件中遍历一长串日期并获取丢失的日期(不包括周末)并将结果写入输出文件。如果缺少的日期是一天,则输出应该是 ccyy/mm/dd ,如果超过一天,则应该是 ccyy/mm/dd - ccyy/mm/dd ,所以这就是我想出的它似乎没有发挥应有的作用,我想我没有正确进行测试。
List<string> missigDateStrings = new List<string>();
for (int i = 0; i < dateList.Count; i++ )
{
DateTime firstDate = dateList[i];
DateTime secondDate = dateList[i + 1];
if (firstDate.DayOfWeek != DayOfWeek.Saturday &&
firstDate.DayOfWeek != DayOfWeek.Sunday)
{
if (secondDate.DayOfWeek != DayOfWeek.Saturday &&
secondDate.DayOfWeek != DayOfWeek.Sunday)
{
if (firstDate.AddDays(1) != secondDate)
{
string sFirstMissingDate = firstDate.ToShortDateString();
DateTime testDate = firstDate;
while (testDate != secondDate)
{
testDate.AddDays(1);
if (testDate == secondDate)
{
string sLastMissingDate = firstDate.AddDays(1).ToShortDateString();
string range = String.Format("{0}-{1}", sFirstMissingDate, sLastMissingDate);
missigDateStrings.Add(range);
}
}
}
}
}
}
}
任何帮助将不胜感激。
PS所有日期都已转换为日期时间,
非常好!谢谢大家
I'm doing this test-app and what it has to do is go through a long list of dates from a text file and get the missing ones (excluding weekend days) and write the results to an output file. If the missing date is a single day the output should be ccyy/mm/dd , if it's more than one day it should be ccyy/mm/dd - ccyy/mm/dd , so this is what I've come up with and it doesn't seem to work as it should, I think I'm not doing the test right.
List<string> missigDateStrings = new List<string>();
for (int i = 0; i < dateList.Count; i++ )
{
DateTime firstDate = dateList[i];
DateTime secondDate = dateList[i + 1];
if (firstDate.DayOfWeek != DayOfWeek.Saturday &&
firstDate.DayOfWeek != DayOfWeek.Sunday)
{
if (secondDate.DayOfWeek != DayOfWeek.Saturday &&
secondDate.DayOfWeek != DayOfWeek.Sunday)
{
if (firstDate.AddDays(1) != secondDate)
{
string sFirstMissingDate = firstDate.ToShortDateString();
DateTime testDate = firstDate;
while (testDate != secondDate)
{
testDate.AddDays(1);
if (testDate == secondDate)
{
string sLastMissingDate = firstDate.AddDays(1).ToShortDateString();
string range = String.Format("{0}-{1}", sFirstMissingDate, sLastMissingDate);
missigDateStrings.Add(range);
}
}
}
}
}
}
}
Any help would be appreciated.
P.S. all the dates have been converted to DateTime
EXCELLENT ! THANKS ALL
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果间隔时间超过 7 天,您也可以享受周末。我认为这是你的问题。因此,您应该添加另一个对周六/周日的检查
,并在周五打破循环,跳过周末并在周一再次开始循环。
编辑:
在您的情况下,以下可能总是错误的。这就是为什么你没有得到任何输出。
If the gap is longer than 7 days you get weekend days also. I assume that is your problem. So you should add another check for Sat/Sun in
and break the loop on Friday, skip weekend and start loop on Monday again.
EDIT:
Below is always false in your case probably. That's why you don't get any output.
更改
为
DateTime 是不可变的值类型,
AddDays()
返回一个新实例,但不会(不能)更改原始值。只是为了好玩,回答标题问题,
Change
to
DateTime is an immutable value-type,
AddDays()
returns a new instance but does not (cannot) change the original value.And just for fun, to answer the title-question,