Link to home
Start Free TrialLog in
Avatar of VMthinker
VMthinker

asked on

C# How to convert String into Time format used for Time Range?

Hi there. I have a program that is able to retrieve sections/part of a log text file's time using tokenization.

The main aim of the program which is to retrieve the time part then proceed onto converting the string to a DateTime format which could be then used as a part of a Time Range timeline function.

However while converting the time into DateTime, the system outputs the results into "23/11/2010 9:31:00 PM" which correctly converts the time into at 12 Hour format but utilizes the Date function.

Therefore the question would be how to only convert the time and NOT output or process the date. And how can the time be converted into a 24 Hour format into HH:MM:SS?

Please advice on the codes. Thanks!

class Program
{
    static void Main(string[] args)
    {

        //System.Collections.Generic.IEnumerable<String> lines = File.ReadLines("C:\\Test\\ntfs2.txt");

        String value = "Thu Mar 02 1995 21:31:00,2245107,m...,r/rrwxrwxrwx,0,0,8349-128-3,C:/Program Files/AccessData/AccessData Forensic Toolkit/Program/wordnet/Adj.dat";

        //foreach (String r in lines)
        //{

        String[] token = value.Split(',');

        String[] datetime = token[0].Split(' ');

        String timeText = datetime[4]; // The String array contans 21:31:00

        DateTime time = Convert.ToDateTime(timeText); // Converts only the time

        Console.WriteLine(time);

    }
}

Open in new window

Avatar of kaufmed
kaufmed
Flag of United States of America image

Change line 21 to:
Console.WriteLine(time.ToString("HH:mm:ss"));  // case-sensitive

Open in new window

Avatar of VMthinker
VMthinker

ASKER

@kaufmed Are there other ways to convert at line 19 instead of the Console.writeline part? As I need to use the time format as a date rnage function.

@rkworlds Don't you think the questions are exactly the same?
Hi,

this should give you the time portion of the date

DateTime.Parse("23/11/2010 9:31:00 PM").TimeOfDay.ToString()
@mmah If you look at my codes I am taking the time out from an array string but not the system.
>>  Are there other ways to convert at line 19 instead of the Console.writeline part?

Console.WriteLine() doesn't have anything to do with converting the date value--it only spits out its parameter to stdout. The ToString() method of the DateTime struct is overloaded to take a format parameter. This specifies how you want the DateTime to appear when output by ToString()--in this case, only the time portion:  HH:mm:ss.
@Kaufmed Are there any other methods or ways that the time text that are in the log text files be converted to be used to time range timeline? Etc. Gather all the logs content form 21:30:45 to 22:46:55??
ASKER CERTIFIED SOLUTION
Avatar of mmahdi
mmahdi

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Note: I didn't show it, but TryParse() returns true or false depending on whether or not the parse was successful (i.e. the timespan is a valid value), so you can check the return values of TryParse() prior to the WriteLine() calls above to be sure you are working with valid time values. Also, if TryParse() succeeds, the "out" parameter will be set to the converted time; otherwise its value will remain unchanged. The "out" keyword is required.
@Kaufmed The timespan and algorith that you showed really helped alot for the time range function! Thanks again!