有如下形式的字符串:
string mytime="1.02:12:22";
如何转换成为TimeSpan类型呢?
方法一:
//如果格式为:[-]d.hh:mm:ss.ff //可以这样转换: public TimeSpan ConvertToTimeSpan( string str ) { int day = 0; int hour = 0; int min = 0; int sec = 0; string strArray1 = str.Split(":"); try { string strArray2 = strArray1[0].Split("."); int day = Convert.ToInt32( strArray2[0] ); int hour = Convert.ToInt32( strArray2[1] ); int min = Convert.ToInt32( strArray1[1] ); int sec = Convert.ToInt32( strArray1[2] ); } catch(Exception) { } return new TimeSpan( day, hour, min, sec ); }
方法二:
TimeSpan.Parse(string)
参考:http://bbs.csdn.net/topics/70133679