Solved

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

Posted on 2010-11-22
11
1,170 Views
Last Modified: 2013-12-14
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

0
Comment
Question by:VMthinker
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
Change line 21 to:
Console.WriteLine(time.ToString("HH:mm:ss"));  // case-sensitive

Open in new window

0
 
LVL 29

Expert Comment

by:Kumaraswamy R
Comment Utility
0
 
LVL 2

Author Comment

by:VMthinker
Comment Utility
@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?
0
 
LVL 1

Expert Comment

by:mmahdi
Comment Utility
Hi,

this should give you the time portion of the date

DateTime.Parse("23/11/2010 9:31:00 PM").TimeOfDay.ToString()
0
 
LVL 2

Author Comment

by:VMthinker
Comment Utility
@mmah If you look at my codes I am taking the time out from an array string but not the system.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
>>  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.
0
 
LVL 2

Author Comment

by:VMthinker
Comment Utility
@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??
0
 
LVL 1

Accepted Solution

by:
mmahdi earned 150 total points
Comment Utility
VMThinker

if you post an example of the flat file maybe I can help. But see below how you can parse your value variable

            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";

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

            foreach (string dateval in token)
            {
                DateTime timeval;
               
                if (DateTime.TryParse(dateval, out timeval))
                {
                    string time = DateTime.Parse(dateval).TimeOfDay.ToString();
                }                
            }
0
 
LVL 74

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 350 total points
Comment Utility
You can use TimeSpan.TryParse:
static void Main(string[] args)

{

    TimeSpan t1, t2, t3, t4;



    TimeSpan.TryParse("08:00:03", out t1);

    TimeSpan.TryParse("09:00:01", out t2);

    TimeSpan.TryParse("08:55:09", out t3);

    TimeSpan.TryParse("07:01:00", out t4);



    Console.WriteLine(t3 >= t1 && t3 <= t2);    // Is t3 between t1 and t2?

    Console.WriteLine(t4 >= t1 && t4 <= t1);    // Is t4 between t1 and t2?



    Console.ReadKey();

}

Open in new window

0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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.
0
 
LVL 2

Author Closing Comment

by:VMthinker
Comment Utility
@Kaufmed The timespan and algorith that you showed really helped alot for the time range function! Thanks again!
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Need help with a query 6 53
.NET Error 7 41
C# SQL BULK INSERT CLASS 5 33
C# Reverse int in fast ways 6 26
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now