StringToTimeSpan

Converts a string to a timespan

源代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
/// <summary>
/// Converts a string time to a timespan.
/// </summary>
/// <param name="time">The time.</param>
/// <returns>
/// A timespan object.
/// </returns>
public static TimeSpan StringToTimeSpan(this string time)
{
TimeSpan timespan;
var result = TimeSpan.TryParse(time, out timespan);
return result ? timespan : new TimeSpan(0, 0, 0);
}

示例:

1
2
3
4
string s = "22:03:34";

// Returns a TimeSpan object with 22 Hours, 3 Minutes and 34 Seconds
TimeSpan ts = s.StringToTimeSpan();
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×