开发者

C# How to create Date and Time Range?

I have a program which processes a log text file, retrieves the date time, converts the date time to the system's DateTime format.

However the program does not seem to recognized the various date time range that is selected. The program should retrieve the records between the date and time selected by the user.

The problem would probably be due to the sequencing of the codes?

May someone please advise on the codes? Thanks!

The Codes:

            String timeDate2 = result1.ToString("MM/dd/yyyy HH:mm:ss");

            Console.WriteLine("The last written time to the registry was : " + timeDate2);

            TimeSpan.TryParse("12/02/2010 16:04:17", out t1);
            TimeSpan.TryParse("12/09/2010 05:12:42", out t2);
            TimeSpan.TryParse(timeDate2, out t3);

            if ((t3 >= t1 && t3 <= t2) == true)    // Is t3 between t1 and t2?
            {
                foreach (String k in lines.Skip(12))
                {
                    Console.WriteLine(k);
                }
             x = 1;
            }

            else
            {
                x = 2;
            }

            Console.WriteLine("============================================================");

        }

        if (x == 2)
        {
            Console.WriteLine("There is no selected days within the log files!");开发者_开发问答
        }


TimeSpan.TryParse("12/02/2010 16:04:17", out t1);
TimeSpan.TryParse("12/09/2010 05:12:42", out t2);
TimeSpan.TryParse(timeDate2, out t3);

TimeSpan normally dont' have a date because its a Time Span, thus how many days, hours and minutes. Not a date. TimeSpan is a Time definition between to dates. More info on the MSDN page

Fix it to DateTime.TryParse

(And check what the values of t1/t2/t3 are after parsing)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜