简体   繁体   中英

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM