如何从范围内获取缺失的日期

发布于 2024-12-03 06:31:35 字数 1470 浏览 0 评论 0原文

我正在做这个测试应用程序,它要做的就是从文本文件中遍历一长串日期并获取丢失的日期(不包括周末)并将结果写入输出文件。如果缺少的日期是一天,则输出应该是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦罢 2024-12-10 06:31:35

如果间隔时间超过 7 天,您也可以享受周末。我认为这是你的问题。因此,您应该添加另一个对周六/周日的检查

while (testDate != secondDate)

,并在周五打破循环,跳过周末并在周一再次开始循环。

编辑:

在您的情况下,以下可能总是错误的。这就是为什么你没有得到任何输出。

if (firstDate == secondDate)

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

while (testDate != secondDate)

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.

if (firstDate == secondDate)
烟织青萝梦 2024-12-10 06:31:35

更改

 testDate.AddDays(1);

 testDate = testDate.AddDays(1);

DateTime 是不可变的值类型,AddDays() 返回一个实例,但不会(不能)更改原始值。

只是为了好玩,回答标题问题,

var missing = Enumerable.Range(0, 10)
          .Select(i => baseDate.AddDays(i))
          .Except(datesFromFile);  

Change

 testDate.AddDays(1);

to

 testDate = testDate.AddDays(1);

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,

var missing = Enumerable.Range(0, 10)
          .Select(i => baseDate.AddDays(i))
          .Except(datesFromFile);  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文