TimeSpan.Parse 方法 (String)
命名空间: System
程序集: mscorlib(在 mscorlib.dll 中)
参数
- s
- 类型:System.String
一个字符串,用于指定进行转换的时间间隔。
| 异常 | 条件 |
|---|---|
| ArgumentNullException | |
| FormatException | |
| OverflowException |
string[] values = { "000000006", "12.12:12:12.12345678" }; foreach (string value in values) { try { TimeSpan interval = TimeSpan.Parse(value); Console.WriteLine("{0} --> {1}", value, interval); } catch (FormatException) { Console.WriteLine("{0}: Bad Format", value); } catch (OverflowException) { Console.WriteLine("{0}: Overflow", value); } } // Output from .NET Framework 3.5 and earlier versions: // 000000006 --> 6.00:00:00 // 12.12:12:12.12345678: Bad Format // Output from .NET Framework 4: // 000000006: Overflow // 12.12:12:12.12345678: Overflow
using System; using System.Globalization; using System.Threading; public class Example { public static void Main() { string[] values = { "6", "6:12", "6:12:14", "6:12:14:45", "6.12:14:45", "6:12:14:45.3448", "6:12:14:45,3448", "6:34:14:45" }; string[] cultureNames = { "hr-HR", "en-US"}; // Change the current culture. foreach (string cultureName in cultureNames) { Thread.CurrentThread.CurrentCulture = new CultureInfo(cultureName); Console.WriteLine("Current Culture: {0}", Thread.CurrentThread.CurrentCulture.Name); foreach (string value in values) { try { TimeSpan ts = TimeSpan.Parse(value); Console.WriteLine("{0} --> {1}", value, ts.ToString("c")); } catch (FormatException) { Console.WriteLine("{0}: Bad Format", value); } catch (OverflowException) { Console.WriteLine("{0}: Overflow", value); } } Console.WriteLine(); } } } // The example displays the following output: // Current Culture: hr-HR // 6 --> 6.00:00:00 // 6:12 --> 06:12:00 // 6:12:14 --> 06:12:14 // 6:12:14:45 --> 6.12:14:45 // 6.12:14:45 --> 6.12:14:45 // 6:12:14:45.3448: Bad Format // 6:12:14:45,3448 --> 6.12:14:45.3448000 // 6:34:14:45: Overflow // // Current Culture: en-US // 6 --> 6.00:00:00 // 6:12 --> 06:12:00 // 6:12:14 --> 06:12:14 // 6:12:14:45 --> 6.12:14:45 // 6.12:14:45 --> 6.12:14:45 // 6:12:14:45.3448 --> 6.12:14:45.3448000 // 6:12:14:45,3448: Bad Format // 6:34:14:45: Overflow
Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008(不支持服务器核心角色), Windows Server 2008 R2(支持带 SP1 或更高版本的服务器核心角色;不支持 Itanium)
.NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。