2

I am trying to convert this string into TimeSpan,

string input = "1h 0m";
string format = "%h %m"; // also tried "hh mm"
TimeSpan ts;
TimeSpan.TryParseExact(input, format, null, out ts);

But output each time I am getting is 00:00:00

Mathematics
  • 7,314
  • 25
  • 77
  • 152

2 Answers2

6

Your format is incorrect. This would work.

string input = "1h 0m";
string[] formats = { @"m\m", @"h\h\ m\m" };
TimeSpan ts;
TimeSpan.TryParseExact(input, formats, null, out ts);

enter image description here

Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208
2
string[] formats = { @"m\m", @"h\h\ m\m" };
RïshïKêsh Kümar
  • 4,734
  • 1
  • 24
  • 36