Why does TimeSpan.ParseExact not work



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








31















This is a bit wierd. Parsing a text field with a valid timespan fails if I try to be precise!



const string tmp = "17:23:24";
//works
var t1 = TimeSpan.Parse(tmp);
//fails
var t2 = TimeSpan.ParseExact(tmp, "hh:mm:ss", System.Globalization.CultureInfo.InvariantCulture);


The second parse fails with an exception "Input string was not in a correct format." from DateTime.










share|improve this question



















  • 5





    Maybe you need HH instead of hh (24 hr format)

    – V4Vendetta
    Jul 30 '12 at 9:53











  • @V4Vendetta: you should post that as an answer; it's likely correct.

    – Dan Puzey
    Jul 30 '12 at 9:54











  • where's your date?

    – John Woo
    Jul 30 '12 at 9:55











  • @JohnTotetWoo: Which date? There's no date in a TimeSpan.

    – Nuffin
    Jul 30 '12 at 9:56






  • 4





    Thanks for all the comments/answers - no HH is NOT a TimeSpan format string, that's not the answer. It's Jon's reply: TimeSpan.ParseExact does not handle separators, which is why it's failing. But Parse does - go figure! You have to escape the : values

    – Quango
    Jul 30 '12 at 10:00

















31















This is a bit wierd. Parsing a text field with a valid timespan fails if I try to be precise!



const string tmp = "17:23:24";
//works
var t1 = TimeSpan.Parse(tmp);
//fails
var t2 = TimeSpan.ParseExact(tmp, "hh:mm:ss", System.Globalization.CultureInfo.InvariantCulture);


The second parse fails with an exception "Input string was not in a correct format." from DateTime.










share|improve this question



















  • 5





    Maybe you need HH instead of hh (24 hr format)

    – V4Vendetta
    Jul 30 '12 at 9:53











  • @V4Vendetta: you should post that as an answer; it's likely correct.

    – Dan Puzey
    Jul 30 '12 at 9:54











  • where's your date?

    – John Woo
    Jul 30 '12 at 9:55











  • @JohnTotetWoo: Which date? There's no date in a TimeSpan.

    – Nuffin
    Jul 30 '12 at 9:56






  • 4





    Thanks for all the comments/answers - no HH is NOT a TimeSpan format string, that's not the answer. It's Jon's reply: TimeSpan.ParseExact does not handle separators, which is why it's failing. But Parse does - go figure! You have to escape the : values

    – Quango
    Jul 30 '12 at 10:00













31












31








31


3






This is a bit wierd. Parsing a text field with a valid timespan fails if I try to be precise!



const string tmp = "17:23:24";
//works
var t1 = TimeSpan.Parse(tmp);
//fails
var t2 = TimeSpan.ParseExact(tmp, "hh:mm:ss", System.Globalization.CultureInfo.InvariantCulture);


The second parse fails with an exception "Input string was not in a correct format." from DateTime.










share|improve this question
















This is a bit wierd. Parsing a text field with a valid timespan fails if I try to be precise!



const string tmp = "17:23:24";
//works
var t1 = TimeSpan.Parse(tmp);
//fails
var t2 = TimeSpan.ParseExact(tmp, "hh:mm:ss", System.Globalization.CultureInfo.InvariantCulture);


The second parse fails with an exception "Input string was not in a correct format." from DateTime.







c# parsing timespan






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 20 '12 at 13:05









Nikhil Agrawal

32.6k1783161




32.6k1783161










asked Jul 30 '12 at 9:52









QuangoQuango

3,60133063




3,60133063







  • 5





    Maybe you need HH instead of hh (24 hr format)

    – V4Vendetta
    Jul 30 '12 at 9:53











  • @V4Vendetta: you should post that as an answer; it's likely correct.

    – Dan Puzey
    Jul 30 '12 at 9:54











  • where's your date?

    – John Woo
    Jul 30 '12 at 9:55











  • @JohnTotetWoo: Which date? There's no date in a TimeSpan.

    – Nuffin
    Jul 30 '12 at 9:56






  • 4





    Thanks for all the comments/answers - no HH is NOT a TimeSpan format string, that's not the answer. It's Jon's reply: TimeSpan.ParseExact does not handle separators, which is why it's failing. But Parse does - go figure! You have to escape the : values

    – Quango
    Jul 30 '12 at 10:00












  • 5





    Maybe you need HH instead of hh (24 hr format)

    – V4Vendetta
    Jul 30 '12 at 9:53











  • @V4Vendetta: you should post that as an answer; it's likely correct.

    – Dan Puzey
    Jul 30 '12 at 9:54











  • where's your date?

    – John Woo
    Jul 30 '12 at 9:55











  • @JohnTotetWoo: Which date? There's no date in a TimeSpan.

    – Nuffin
    Jul 30 '12 at 9:56






  • 4





    Thanks for all the comments/answers - no HH is NOT a TimeSpan format string, that's not the answer. It's Jon's reply: TimeSpan.ParseExact does not handle separators, which is why it's failing. But Parse does - go figure! You have to escape the : values

    – Quango
    Jul 30 '12 at 10:00







5




5





Maybe you need HH instead of hh (24 hr format)

– V4Vendetta
Jul 30 '12 at 9:53





Maybe you need HH instead of hh (24 hr format)

– V4Vendetta
Jul 30 '12 at 9:53













@V4Vendetta: you should post that as an answer; it's likely correct.

– Dan Puzey
Jul 30 '12 at 9:54





@V4Vendetta: you should post that as an answer; it's likely correct.

– Dan Puzey
Jul 30 '12 at 9:54













where's your date?

– John Woo
Jul 30 '12 at 9:55





where's your date?

– John Woo
Jul 30 '12 at 9:55













@JohnTotetWoo: Which date? There's no date in a TimeSpan.

– Nuffin
Jul 30 '12 at 9:56





@JohnTotetWoo: Which date? There's no date in a TimeSpan.

– Nuffin
Jul 30 '12 at 9:56




4




4





Thanks for all the comments/answers - no HH is NOT a TimeSpan format string, that's not the answer. It's Jon's reply: TimeSpan.ParseExact does not handle separators, which is why it's failing. But Parse does - go figure! You have to escape the : values

– Quango
Jul 30 '12 at 10:00





Thanks for all the comments/answers - no HH is NOT a TimeSpan format string, that's not the answer. It's Jon's reply: TimeSpan.ParseExact does not handle separators, which is why it's failing. But Parse does - go figure! You have to escape the : values

– Quango
Jul 30 '12 at 10:00












5 Answers
5






active

oldest

votes


















69














From the documentation:




Any other unescaped character in a format string, including a
white-space character, is interpreted as a custom format specifier. In
most cases, the presence of any other unescaped character results in a
FormatException.



There are two ways to include a literal character in a format string:



  • Enclose it in single quotation marks (the literal string delimiter).


  • Precede it with a backslash (""), which is interpreted as an escape character. This means that, in C#, the format string must
    either be @-quoted, or the literal character must be preceded by an
    additional backslash.


The .NET Framework does not define a grammar for separators in time
intervals. This means that the separators between days and hours,
hours and minutes, minutes and seconds, and seconds and fractions of a
second must all be treated as character literals in a format string.




So, the solution is to specify the format string as



TimeSpan.ParseExact(tmp, "hh\:mm\:ss", CultureInfo.InvariantCulture)





share|improve this answer


















  • 5





    Nice answer, Jon - I'd never realised that ParseExact ignores the 12/24 hour convention with the formatters.

    – Dan Puzey
    Jul 30 '12 at 10:01











  • So it is - learned something new today! Odd that Parse handles separators but ParseExact doesn't!

    – Quango
    Jul 30 '12 at 10:02











  • I was hoping this would solve my problem related to converting from string to TimeSpan in the Expression string of DataTable.Select(). However, no luck. Filed a bug here.

    – Conrad
    Dec 4 '13 at 14:09






  • 1





    you could also use @"hh:mm:ss".

    – Peter
    Oct 14 '16 at 14:22






  • 1





    After all, it would be too easy if it just worked... That's just grotesque...

    – Stefan Steiger
    Nov 2 '16 at 9:44


















8














Try this:



var t2 = TimeSpan.ParseExact(tmp, "c", System.Globalization.CultureInfo.InvariantCulture);


Source:
Standard TimeSpan Format Strings






share|improve this answer























  • Thanks for the reply - I hadn't noticed the 'c' format, which takes care of the separator issue: another new thing learned!

    – Quango
    Jul 30 '12 at 10:11


















4














If you don't want to deal with the difference in format specifiers between TimeSpan.ParseExact and DateTime.ParseExact you can just parse your string as a DateTime and get the TimeOfDay component as a TimeSpan like this:



var t2 = DateTime.ParseExact(tmp, "hh:mm:ss", CultureInfo.InvariantCulture).TimeOfDay;





share|improve this answer























  • See this answer for how to go the other way: formatting a TimeSpan using DateTime format specifiers.

    – samgak
    Jun 28 '17 at 4:20











  • thank you. It was really helpful.

    – Obaid
    Jan 30 at 18:08


















2














It seems that HH is not really for TimeSpan




The custom TimeSpan format specifiers do not include placeholder
separator symbols, such as the symbols that separate days from hours,
hours from minutes, or seconds from fractional seconds. Instead, these
symbols must be included in the custom format string as string
literals. For example, "dd.hh:mm" defines a period (.) as the
separator between days and hours, and a colon (:) as the separator
between hours and minutes.




Hence the correct way would be as Jon mentioned to escape using "" Read More




Your TimeSpan is "17:23:24" which is in the 24 hour format and it should be parsed using HH format and not hh which is for 12 hour formats.



TimeSpan.ParseExact(tmp, "HH:mm:ss",System.Globalization.CultureInfo.InvariantCulture);


Check out the formats






share|improve this answer

























  • Not relevant: the specifier for hours that are not counted as part of days is hh.

    – Jon
    Jul 30 '12 at 9:58











  • @Jon What do you mean ?

    – V4Vendetta
    Jul 30 '12 at 10:00






  • 1





    You are linking to the DateTime format strings, which are different than the TimeSpan format strings.

    – Jon
    Jul 30 '12 at 10:02






  • 2





    Nope, not correct. Timespan does not support HH, it only supports hh

    – Quango
    Jul 30 '12 at 10:02











  • @Jon Indeed that's the case, real eye opener for me, thank you

    – V4Vendetta
    Jul 30 '12 at 10:10


















0














Try this:



 var t2 = TimeSpan.ParseExact(tmp, "HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);





share|improve this answer























  • Thanks for the reply - see Jon's answer for the actual problem.

    – Quango
    Jul 30 '12 at 10:09











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f11719055%2fwhy-does-timespan-parseexact-not-work%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























5 Answers
5






active

oldest

votes








5 Answers
5






active

oldest

votes









active

oldest

votes






active

oldest

votes









69














From the documentation:




Any other unescaped character in a format string, including a
white-space character, is interpreted as a custom format specifier. In
most cases, the presence of any other unescaped character results in a
FormatException.



There are two ways to include a literal character in a format string:



  • Enclose it in single quotation marks (the literal string delimiter).


  • Precede it with a backslash (""), which is interpreted as an escape character. This means that, in C#, the format string must
    either be @-quoted, or the literal character must be preceded by an
    additional backslash.


The .NET Framework does not define a grammar for separators in time
intervals. This means that the separators between days and hours,
hours and minutes, minutes and seconds, and seconds and fractions of a
second must all be treated as character literals in a format string.




So, the solution is to specify the format string as



TimeSpan.ParseExact(tmp, "hh\:mm\:ss", CultureInfo.InvariantCulture)





share|improve this answer


















  • 5





    Nice answer, Jon - I'd never realised that ParseExact ignores the 12/24 hour convention with the formatters.

    – Dan Puzey
    Jul 30 '12 at 10:01











  • So it is - learned something new today! Odd that Parse handles separators but ParseExact doesn't!

    – Quango
    Jul 30 '12 at 10:02











  • I was hoping this would solve my problem related to converting from string to TimeSpan in the Expression string of DataTable.Select(). However, no luck. Filed a bug here.

    – Conrad
    Dec 4 '13 at 14:09






  • 1





    you could also use @"hh:mm:ss".

    – Peter
    Oct 14 '16 at 14:22






  • 1





    After all, it would be too easy if it just worked... That's just grotesque...

    – Stefan Steiger
    Nov 2 '16 at 9:44















69














From the documentation:




Any other unescaped character in a format string, including a
white-space character, is interpreted as a custom format specifier. In
most cases, the presence of any other unescaped character results in a
FormatException.



There are two ways to include a literal character in a format string:



  • Enclose it in single quotation marks (the literal string delimiter).


  • Precede it with a backslash (""), which is interpreted as an escape character. This means that, in C#, the format string must
    either be @-quoted, or the literal character must be preceded by an
    additional backslash.


The .NET Framework does not define a grammar for separators in time
intervals. This means that the separators between days and hours,
hours and minutes, minutes and seconds, and seconds and fractions of a
second must all be treated as character literals in a format string.




So, the solution is to specify the format string as



TimeSpan.ParseExact(tmp, "hh\:mm\:ss", CultureInfo.InvariantCulture)





share|improve this answer


















  • 5





    Nice answer, Jon - I'd never realised that ParseExact ignores the 12/24 hour convention with the formatters.

    – Dan Puzey
    Jul 30 '12 at 10:01











  • So it is - learned something new today! Odd that Parse handles separators but ParseExact doesn't!

    – Quango
    Jul 30 '12 at 10:02











  • I was hoping this would solve my problem related to converting from string to TimeSpan in the Expression string of DataTable.Select(). However, no luck. Filed a bug here.

    – Conrad
    Dec 4 '13 at 14:09






  • 1





    you could also use @"hh:mm:ss".

    – Peter
    Oct 14 '16 at 14:22






  • 1





    After all, it would be too easy if it just worked... That's just grotesque...

    – Stefan Steiger
    Nov 2 '16 at 9:44













69












69








69







From the documentation:




Any other unescaped character in a format string, including a
white-space character, is interpreted as a custom format specifier. In
most cases, the presence of any other unescaped character results in a
FormatException.



There are two ways to include a literal character in a format string:



  • Enclose it in single quotation marks (the literal string delimiter).


  • Precede it with a backslash (""), which is interpreted as an escape character. This means that, in C#, the format string must
    either be @-quoted, or the literal character must be preceded by an
    additional backslash.


The .NET Framework does not define a grammar for separators in time
intervals. This means that the separators between days and hours,
hours and minutes, minutes and seconds, and seconds and fractions of a
second must all be treated as character literals in a format string.




So, the solution is to specify the format string as



TimeSpan.ParseExact(tmp, "hh\:mm\:ss", CultureInfo.InvariantCulture)





share|improve this answer













From the documentation:




Any other unescaped character in a format string, including a
white-space character, is interpreted as a custom format specifier. In
most cases, the presence of any other unescaped character results in a
FormatException.



There are two ways to include a literal character in a format string:



  • Enclose it in single quotation marks (the literal string delimiter).


  • Precede it with a backslash (""), which is interpreted as an escape character. This means that, in C#, the format string must
    either be @-quoted, or the literal character must be preceded by an
    additional backslash.


The .NET Framework does not define a grammar for separators in time
intervals. This means that the separators between days and hours,
hours and minutes, minutes and seconds, and seconds and fractions of a
second must all be treated as character literals in a format string.




So, the solution is to specify the format string as



TimeSpan.ParseExact(tmp, "hh\:mm\:ss", CultureInfo.InvariantCulture)






share|improve this answer












share|improve this answer



share|improve this answer










answered Jul 30 '12 at 9:57









JonJon

348k60615719




348k60615719







  • 5





    Nice answer, Jon - I'd never realised that ParseExact ignores the 12/24 hour convention with the formatters.

    – Dan Puzey
    Jul 30 '12 at 10:01











  • So it is - learned something new today! Odd that Parse handles separators but ParseExact doesn't!

    – Quango
    Jul 30 '12 at 10:02











  • I was hoping this would solve my problem related to converting from string to TimeSpan in the Expression string of DataTable.Select(). However, no luck. Filed a bug here.

    – Conrad
    Dec 4 '13 at 14:09






  • 1





    you could also use @"hh:mm:ss".

    – Peter
    Oct 14 '16 at 14:22






  • 1





    After all, it would be too easy if it just worked... That's just grotesque...

    – Stefan Steiger
    Nov 2 '16 at 9:44












  • 5





    Nice answer, Jon - I'd never realised that ParseExact ignores the 12/24 hour convention with the formatters.

    – Dan Puzey
    Jul 30 '12 at 10:01











  • So it is - learned something new today! Odd that Parse handles separators but ParseExact doesn't!

    – Quango
    Jul 30 '12 at 10:02











  • I was hoping this would solve my problem related to converting from string to TimeSpan in the Expression string of DataTable.Select(). However, no luck. Filed a bug here.

    – Conrad
    Dec 4 '13 at 14:09






  • 1





    you could also use @"hh:mm:ss".

    – Peter
    Oct 14 '16 at 14:22






  • 1





    After all, it would be too easy if it just worked... That's just grotesque...

    – Stefan Steiger
    Nov 2 '16 at 9:44







5




5





Nice answer, Jon - I'd never realised that ParseExact ignores the 12/24 hour convention with the formatters.

– Dan Puzey
Jul 30 '12 at 10:01





Nice answer, Jon - I'd never realised that ParseExact ignores the 12/24 hour convention with the formatters.

– Dan Puzey
Jul 30 '12 at 10:01













So it is - learned something new today! Odd that Parse handles separators but ParseExact doesn't!

– Quango
Jul 30 '12 at 10:02





So it is - learned something new today! Odd that Parse handles separators but ParseExact doesn't!

– Quango
Jul 30 '12 at 10:02













I was hoping this would solve my problem related to converting from string to TimeSpan in the Expression string of DataTable.Select(). However, no luck. Filed a bug here.

– Conrad
Dec 4 '13 at 14:09





I was hoping this would solve my problem related to converting from string to TimeSpan in the Expression string of DataTable.Select(). However, no luck. Filed a bug here.

– Conrad
Dec 4 '13 at 14:09




1




1





you could also use @"hh:mm:ss".

– Peter
Oct 14 '16 at 14:22





you could also use @"hh:mm:ss".

– Peter
Oct 14 '16 at 14:22




1




1





After all, it would be too easy if it just worked... That's just grotesque...

– Stefan Steiger
Nov 2 '16 at 9:44





After all, it would be too easy if it just worked... That's just grotesque...

– Stefan Steiger
Nov 2 '16 at 9:44













8














Try this:



var t2 = TimeSpan.ParseExact(tmp, "c", System.Globalization.CultureInfo.InvariantCulture);


Source:
Standard TimeSpan Format Strings






share|improve this answer























  • Thanks for the reply - I hadn't noticed the 'c' format, which takes care of the separator issue: another new thing learned!

    – Quango
    Jul 30 '12 at 10:11















8














Try this:



var t2 = TimeSpan.ParseExact(tmp, "c", System.Globalization.CultureInfo.InvariantCulture);


Source:
Standard TimeSpan Format Strings






share|improve this answer























  • Thanks for the reply - I hadn't noticed the 'c' format, which takes care of the separator issue: another new thing learned!

    – Quango
    Jul 30 '12 at 10:11













8












8








8







Try this:



var t2 = TimeSpan.ParseExact(tmp, "c", System.Globalization.CultureInfo.InvariantCulture);


Source:
Standard TimeSpan Format Strings






share|improve this answer













Try this:



var t2 = TimeSpan.ParseExact(tmp, "c", System.Globalization.CultureInfo.InvariantCulture);


Source:
Standard TimeSpan Format Strings







share|improve this answer












share|improve this answer



share|improve this answer










answered Jul 30 '12 at 10:01









M. Mennan KaraM. Mennan Kara

8,70412734




8,70412734












  • Thanks for the reply - I hadn't noticed the 'c' format, which takes care of the separator issue: another new thing learned!

    – Quango
    Jul 30 '12 at 10:11

















  • Thanks for the reply - I hadn't noticed the 'c' format, which takes care of the separator issue: another new thing learned!

    – Quango
    Jul 30 '12 at 10:11
















Thanks for the reply - I hadn't noticed the 'c' format, which takes care of the separator issue: another new thing learned!

– Quango
Jul 30 '12 at 10:11





Thanks for the reply - I hadn't noticed the 'c' format, which takes care of the separator issue: another new thing learned!

– Quango
Jul 30 '12 at 10:11











4














If you don't want to deal with the difference in format specifiers between TimeSpan.ParseExact and DateTime.ParseExact you can just parse your string as a DateTime and get the TimeOfDay component as a TimeSpan like this:



var t2 = DateTime.ParseExact(tmp, "hh:mm:ss", CultureInfo.InvariantCulture).TimeOfDay;





share|improve this answer























  • See this answer for how to go the other way: formatting a TimeSpan using DateTime format specifiers.

    – samgak
    Jun 28 '17 at 4:20











  • thank you. It was really helpful.

    – Obaid
    Jan 30 at 18:08















4














If you don't want to deal with the difference in format specifiers between TimeSpan.ParseExact and DateTime.ParseExact you can just parse your string as a DateTime and get the TimeOfDay component as a TimeSpan like this:



var t2 = DateTime.ParseExact(tmp, "hh:mm:ss", CultureInfo.InvariantCulture).TimeOfDay;





share|improve this answer























  • See this answer for how to go the other way: formatting a TimeSpan using DateTime format specifiers.

    – samgak
    Jun 28 '17 at 4:20











  • thank you. It was really helpful.

    – Obaid
    Jan 30 at 18:08













4












4








4







If you don't want to deal with the difference in format specifiers between TimeSpan.ParseExact and DateTime.ParseExact you can just parse your string as a DateTime and get the TimeOfDay component as a TimeSpan like this:



var t2 = DateTime.ParseExact(tmp, "hh:mm:ss", CultureInfo.InvariantCulture).TimeOfDay;





share|improve this answer













If you don't want to deal with the difference in format specifiers between TimeSpan.ParseExact and DateTime.ParseExact you can just parse your string as a DateTime and get the TimeOfDay component as a TimeSpan like this:



var t2 = DateTime.ParseExact(tmp, "hh:mm:ss", CultureInfo.InvariantCulture).TimeOfDay;






share|improve this answer












share|improve this answer



share|improve this answer










answered Jun 28 '17 at 4:00









samgaksamgak

19.2k33362




19.2k33362












  • See this answer for how to go the other way: formatting a TimeSpan using DateTime format specifiers.

    – samgak
    Jun 28 '17 at 4:20











  • thank you. It was really helpful.

    – Obaid
    Jan 30 at 18:08

















  • See this answer for how to go the other way: formatting a TimeSpan using DateTime format specifiers.

    – samgak
    Jun 28 '17 at 4:20











  • thank you. It was really helpful.

    – Obaid
    Jan 30 at 18:08
















See this answer for how to go the other way: formatting a TimeSpan using DateTime format specifiers.

– samgak
Jun 28 '17 at 4:20





See this answer for how to go the other way: formatting a TimeSpan using DateTime format specifiers.

– samgak
Jun 28 '17 at 4:20













thank you. It was really helpful.

– Obaid
Jan 30 at 18:08





thank you. It was really helpful.

– Obaid
Jan 30 at 18:08











2














It seems that HH is not really for TimeSpan




The custom TimeSpan format specifiers do not include placeholder
separator symbols, such as the symbols that separate days from hours,
hours from minutes, or seconds from fractional seconds. Instead, these
symbols must be included in the custom format string as string
literals. For example, "dd.hh:mm" defines a period (.) as the
separator between days and hours, and a colon (:) as the separator
between hours and minutes.




Hence the correct way would be as Jon mentioned to escape using "" Read More




Your TimeSpan is "17:23:24" which is in the 24 hour format and it should be parsed using HH format and not hh which is for 12 hour formats.



TimeSpan.ParseExact(tmp, "HH:mm:ss",System.Globalization.CultureInfo.InvariantCulture);


Check out the formats






share|improve this answer

























  • Not relevant: the specifier for hours that are not counted as part of days is hh.

    – Jon
    Jul 30 '12 at 9:58











  • @Jon What do you mean ?

    – V4Vendetta
    Jul 30 '12 at 10:00






  • 1





    You are linking to the DateTime format strings, which are different than the TimeSpan format strings.

    – Jon
    Jul 30 '12 at 10:02






  • 2





    Nope, not correct. Timespan does not support HH, it only supports hh

    – Quango
    Jul 30 '12 at 10:02











  • @Jon Indeed that's the case, real eye opener for me, thank you

    – V4Vendetta
    Jul 30 '12 at 10:10















2














It seems that HH is not really for TimeSpan




The custom TimeSpan format specifiers do not include placeholder
separator symbols, such as the symbols that separate days from hours,
hours from minutes, or seconds from fractional seconds. Instead, these
symbols must be included in the custom format string as string
literals. For example, "dd.hh:mm" defines a period (.) as the
separator between days and hours, and a colon (:) as the separator
between hours and minutes.




Hence the correct way would be as Jon mentioned to escape using "" Read More




Your TimeSpan is "17:23:24" which is in the 24 hour format and it should be parsed using HH format and not hh which is for 12 hour formats.



TimeSpan.ParseExact(tmp, "HH:mm:ss",System.Globalization.CultureInfo.InvariantCulture);


Check out the formats






share|improve this answer

























  • Not relevant: the specifier for hours that are not counted as part of days is hh.

    – Jon
    Jul 30 '12 at 9:58











  • @Jon What do you mean ?

    – V4Vendetta
    Jul 30 '12 at 10:00






  • 1





    You are linking to the DateTime format strings, which are different than the TimeSpan format strings.

    – Jon
    Jul 30 '12 at 10:02






  • 2





    Nope, not correct. Timespan does not support HH, it only supports hh

    – Quango
    Jul 30 '12 at 10:02











  • @Jon Indeed that's the case, real eye opener for me, thank you

    – V4Vendetta
    Jul 30 '12 at 10:10













2












2








2







It seems that HH is not really for TimeSpan




The custom TimeSpan format specifiers do not include placeholder
separator symbols, such as the symbols that separate days from hours,
hours from minutes, or seconds from fractional seconds. Instead, these
symbols must be included in the custom format string as string
literals. For example, "dd.hh:mm" defines a period (.) as the
separator between days and hours, and a colon (:) as the separator
between hours and minutes.




Hence the correct way would be as Jon mentioned to escape using "" Read More




Your TimeSpan is "17:23:24" which is in the 24 hour format and it should be parsed using HH format and not hh which is for 12 hour formats.



TimeSpan.ParseExact(tmp, "HH:mm:ss",System.Globalization.CultureInfo.InvariantCulture);


Check out the formats






share|improve this answer















It seems that HH is not really for TimeSpan




The custom TimeSpan format specifiers do not include placeholder
separator symbols, such as the symbols that separate days from hours,
hours from minutes, or seconds from fractional seconds. Instead, these
symbols must be included in the custom format string as string
literals. For example, "dd.hh:mm" defines a period (.) as the
separator between days and hours, and a colon (:) as the separator
between hours and minutes.




Hence the correct way would be as Jon mentioned to escape using "" Read More




Your TimeSpan is "17:23:24" which is in the 24 hour format and it should be parsed using HH format and not hh which is for 12 hour formats.



TimeSpan.ParseExact(tmp, "HH:mm:ss",System.Globalization.CultureInfo.InvariantCulture);


Check out the formats







share|improve this answer














share|improve this answer



share|improve this answer








edited Jul 30 '12 at 10:09

























answered Jul 30 '12 at 9:55









V4VendettaV4Vendetta

29.1k66477




29.1k66477












  • Not relevant: the specifier for hours that are not counted as part of days is hh.

    – Jon
    Jul 30 '12 at 9:58











  • @Jon What do you mean ?

    – V4Vendetta
    Jul 30 '12 at 10:00






  • 1





    You are linking to the DateTime format strings, which are different than the TimeSpan format strings.

    – Jon
    Jul 30 '12 at 10:02






  • 2





    Nope, not correct. Timespan does not support HH, it only supports hh

    – Quango
    Jul 30 '12 at 10:02











  • @Jon Indeed that's the case, real eye opener for me, thank you

    – V4Vendetta
    Jul 30 '12 at 10:10

















  • Not relevant: the specifier for hours that are not counted as part of days is hh.

    – Jon
    Jul 30 '12 at 9:58











  • @Jon What do you mean ?

    – V4Vendetta
    Jul 30 '12 at 10:00






  • 1





    You are linking to the DateTime format strings, which are different than the TimeSpan format strings.

    – Jon
    Jul 30 '12 at 10:02






  • 2





    Nope, not correct. Timespan does not support HH, it only supports hh

    – Quango
    Jul 30 '12 at 10:02











  • @Jon Indeed that's the case, real eye opener for me, thank you

    – V4Vendetta
    Jul 30 '12 at 10:10
















Not relevant: the specifier for hours that are not counted as part of days is hh.

– Jon
Jul 30 '12 at 9:58





Not relevant: the specifier for hours that are not counted as part of days is hh.

– Jon
Jul 30 '12 at 9:58













@Jon What do you mean ?

– V4Vendetta
Jul 30 '12 at 10:00





@Jon What do you mean ?

– V4Vendetta
Jul 30 '12 at 10:00




1




1





You are linking to the DateTime format strings, which are different than the TimeSpan format strings.

– Jon
Jul 30 '12 at 10:02





You are linking to the DateTime format strings, which are different than the TimeSpan format strings.

– Jon
Jul 30 '12 at 10:02




2




2





Nope, not correct. Timespan does not support HH, it only supports hh

– Quango
Jul 30 '12 at 10:02





Nope, not correct. Timespan does not support HH, it only supports hh

– Quango
Jul 30 '12 at 10:02













@Jon Indeed that's the case, real eye opener for me, thank you

– V4Vendetta
Jul 30 '12 at 10:10





@Jon Indeed that's the case, real eye opener for me, thank you

– V4Vendetta
Jul 30 '12 at 10:10











0














Try this:



 var t2 = TimeSpan.ParseExact(tmp, "HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);





share|improve this answer























  • Thanks for the reply - see Jon's answer for the actual problem.

    – Quango
    Jul 30 '12 at 10:09















0














Try this:



 var t2 = TimeSpan.ParseExact(tmp, "HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);





share|improve this answer























  • Thanks for the reply - see Jon's answer for the actual problem.

    – Quango
    Jul 30 '12 at 10:09













0












0








0







Try this:



 var t2 = TimeSpan.ParseExact(tmp, "HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);





share|improve this answer













Try this:



 var t2 = TimeSpan.ParseExact(tmp, "HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);






share|improve this answer












share|improve this answer



share|improve this answer










answered Jul 30 '12 at 9:56









speti43speti43

1,65111319




1,65111319












  • Thanks for the reply - see Jon's answer for the actual problem.

    – Quango
    Jul 30 '12 at 10:09

















  • Thanks for the reply - see Jon's answer for the actual problem.

    – Quango
    Jul 30 '12 at 10:09
















Thanks for the reply - see Jon's answer for the actual problem.

– Quango
Jul 30 '12 at 10:09





Thanks for the reply - see Jon's answer for the actual problem.

– Quango
Jul 30 '12 at 10:09

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f11719055%2fwhy-does-timespan-parseexact-not-work%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo