Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi ! I searched how to convert an string into time format in c# but I am not obtaning the desired result.

The string received follows next format "hhmmss.ms" and I want to toconvert it in hh:mm:ss or hh:mm:ss:ms

Some of the things that I checked with no success were:
1)
C#
double ConvTime = Convert.ToDouble(mystring);
this.TimeText.Text = String.Format("{0:00.000}", ConvTime);

2)
C#
string [] time = mystring.split(".");
DateTime dateTime = DateTime.ParseExact(time[0], "hh:mm:ss",
                                             CultureInfo.InvariantCulture);
this.TimeText.Text =  dateTime.ToString();
//or
this.TimeText.Text =  dateTime.ToString("T", CultureInfo.CurrentCulture);


3)
C#
TimeSpan Time = TimeSpan.FromHours(Convert.ToDouble(mystring));
this.TimeText.Text =  Convert.ToString(Time);


Any help?
Posted

Try:
C#
TimeSpan ts;
if (TimeSpan.TryParseExact("143522.666", @"hhmmss\.fff", CultureInfo.InvariantCulture, out ts))
    {
    Console.WriteLine(ts);
    }
 
Share this answer
 
Comments
Kinna-10626331 28-Nov-14 8:14am    
Thanks!!! , works :)
OriginalGriff 28-Nov-14 8:22am    
You're welcome!
Try this.

TimeSpan time = TimeSpan.Parse(timeString)
 
Share this answer
 
v2
Comments
OriginalGriff 28-Nov-14 6:42am    
Reason for my vote of one: Did you try this with a sample of his time value?
And what exception did you get? :laugh:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900