Convert string to TimeSpan doesn't work

516 Views Asked by At

i'm trying to convert a string into timeSpan but i can't seem to do it.

i'm using c++ managed code:

TimeSpan timeSpan;
if (TimeSpan::TryParse("01.55", timeSpan))
{  
  int minute = timeSpan.Minutes;
  int hours= timeSpan.Hours;
  //do some work here
}

the TryParse is returning flase. what am i doing wrong ?

Thank you,

1

There are 1 best solutions below

5
On

The time format is wrong, see MSDN TimeSpan::TryParse.

The format should be: [ws][-]{ d | d.hh:mm[:ss[.ff]] | hh:mm[:ss[.ff]] }[ws]

Or short for your example 1:55 instead of 1.55. This is format for 1 hour and 55 minutes. Your notation is for days.hours, which is also wrong as day has only 24 hours so TimeSpan allows max value of 23. Your string still needs :0:0 so it forms dd.hh:mm:ss to be successfully parsed.