4

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!");
        }
JavaNoob
  • 3,494
  • 19
  • 49
  • 61
  • == true is redundant, also are you sure 'lines' has more than 12 lines? – GreyCloud Dec 14 '10 at 09:39
  • Verify the (bool) result of the TryParse (should be `true` if the text was correctly decoded) and check the "out" value, is that the date and/or time you expected? – Hans Kesting Dec 14 '10 at 09:47

1 Answers1

1
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)

RvdK
  • 19,580
  • 4
  • 64
  • 107
  • The program is using DateTime.TryParseExact and the results would be etc. "12/09/2010 05:12:42". – JavaNoob Dec 14 '10 at 09:39
  • The parameter of TimeSpan.TryParse should be in the format of '[ws][-]{ d | d.hh:mm[:ss[.ff]] | hh:mm[:ss[.ff]] }[ws]' such as "99.23:59:59" for 99 days, 23 hours, 59 minutes and 59seconds. – RvdK Dec 14 '10 at 09:42
  • Sorry didn't read it properly just now the results for t1: "12/2/2010 4:04:17 PM" t2: "12/5/2010 5:12:42 AM" and timeDate: "12/09/2010 05:12:42" after the TimeSpan was changed to DateTime. – JavaNoob Dec 14 '10 at 09:46
  • @JavaNoob: if the real program used DateTime.TryParse(-Exact), why use TimeSpan.TryParse in your example? – Hans Kesting Dec 14 '10 at 09:48
  • Maybe also good to rewrite the if statement to "if ((t3 >= t1) && (t3 <= t2))". == true is redundant and note the extra parentheses to clearify. – RvdK Dec 14 '10 at 09:51
  • @Hans I was unsure about the TimeSpan usage as it included days but not dates until Powe mentioned the difference. – JavaNoob Dec 15 '10 at 04:22