Sunday, 15 January 2012

c# - How to parse hours,minutes and seconds from a string to a TimeSpan object? -



c# - How to parse hours,minutes and seconds from a string to a TimeSpan object? -

i'm passing string unit of time via query string. when seek parse string time span object system.formatexception' occurred in mscorlib.ni.dll not handled in user code gather means there problem way i'm formatting string parsed.

if (navigationcontext.querystring.containskey("worktimespanpkr")) { teststring = navigationcontext.querystring["worktimespanpkr"]; //assign text box string value test time span variable. testtm = timespan.parseexact(teststring, @"hh\ \:\ mm\ \:\ ss", cultureinfo.invariantculture); }

the string beingness passed on teststring when run through debugger is: `"00:15:04"``

does know right format parsing hours,minutes , seconds?

this value i'm trying parse , code i'm using accomplish this:

the next works fine me:

console.writeline(timespan.parseexact("00:15:04", @"hh\:mm\:ss", cultureinfo.invariantculture, timespanstyles.none));

you should remove whitespace format string if want match illustration of 00:15:04.

also may want read http://msdn.microsoft.com/en-us/library/ee372287(v=vs.110).aspx

c# parsing timespan string-formatting

No comments:

Post a Comment