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

I have a program that can extract sections / part time of a text log file using tokenization.

The main goal of the program, which is to extract part of the time, then move on to converting the string to DateTime format, which can then be used as part of the Time Range timeline.

However, converting the time to DateTime, the system displays the results in "11/23/2010 9:31:00 PM", which correctly converts the time into a 12-hour format, but uses the Date function.

So the question is how to convert the time and NOT output or process the date. And how can I convert the time into a 24-hour format in HH: MM: SS?

Consult 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); } } 
+4
source share
6 answers
  //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"; 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.ToString("HH:mm:ss")); 

You can use DateTime.ToString ("pattern") to convert DateTime to any format you want.

Here is a list of the templates available here http://www.geekzilla.co.uk/View00FF7904-B510-468C-A2C8-F859AA20581F.htm

+7
source

just use this code to convert the time date, e.g. from 5.12 to 1712

 public string ConvDate_as_str(string dateFormat) { try { char[] ch = dateFormat.ToCharArray(); string[] sps = dateFormat.Split(' '); string[] spd = sps[0].Split('.'); dateFormat = spd[0] + ":" + spd[1]; if ((sps[1].ToUpper() != "PM") && (sps[1].ToUpper() != "AM")) { return "Enter Correct Format like <5.12 pm>"; } DateTime dt = new DateTime(); dt = Convert.ToDateTime(dateFormat); string date = ""; if (sps[1].ToUpper() == "PM") { date = (dt.Hour + 12).ToString("00"); } else date = dt.Hour.ToString("00"); return date + dt.Minute.ToString("00"); } catch (Exception ex) { return "Enter Correct Format like <5.12 pm>"; } } 
+2
source
 //your log record string record = "Thu Mar 02 1995 21:31:00,2245107..."; //get the first part - you can include the Thu Mar 02 1995 (datetime can parse) string date = record.Split(',')[0]; //parse to date time (the Thu Mar 02 doesn't phase dateTime) DateTime dt = DateTime.Parse(date); //convert the datetime to a string HH = 24 Hours leading zero, mm = minutes, ss = seconds string time = dt.ToString("HH:mm:ss"); //write! Console.WriteLine(time); 

edit I see that you say that you are not processing the date, it technically processes it, but it is much cleaner than separating based on spaces and re-arranging time in parts.

+1
source
 DateTime x = DateTime.Parse("01/01/2010 10:45 PM"); Console.WriteLine(x.ToString("HH:mm")); 
0
source

Just use this code to convert string to time

 try { string hour = stringFormat.Substring(0, 2); string min = stringFormat.Substring(2, 2); DateTime dt = new DateTime(); dt = Convert.ToDateTime(hour+":"+min); return String.Format("{0:t}", dt); ; } catch (Exception ex) { return "Please Enter Correct format like <0559>"; } 
0
source

Why is it so complicated this is my code:

  private void timer7_Tick(object sender, EventArgs e) { label4.Text = DateTime.Now.TimeOfDay.Hours.ToString() + ":" + DateTime.Now.TimeOfDay.Minutes.ToString() + ":" + DateTime.Now.TimeOfDay.Seconds.ToString() + ":" + DateTime.Now.TimeOfDay.TotalMilliseconds.ToString(); label7.Text = DateTime.Now.Date.Year.ToString() + "-" + DateTime.Now.Date.Month.ToString() + "-" + DateTime.Now.Date.Day.ToString(); } 
0
source

All Articles