导出 (0) 打印
全部展开
此文章由机器翻译。 将光标移到文章的句子上,以查看原文。 更多信息。
译文
原文

TimeSpan.Parse 方法 (String)

将时间间隔的字符串表示形式转换为等效的 TimeSpan

命名空间:  System
程序集:  mscorlib(在 mscorlib.dll 中)

public static TimeSpan Parse(
	string s
)

参数

s
类型:System.String
一个字符串,用于指定进行转换的时间间隔。

返回值

类型:System.TimeSpan
s 对应的时间间隔。

异常条件
ArgumentNullException

snull

FormatException

s 具有无效的格式。

OverflowException

s 表示小于 TimeSpan.MinValue 或大于 TimeSpan.MaxValue 的数字。

- 或 -

天、小时、分钟或秒中至少有一个分量超出其有效范围。

The s parameter contains a time interval specification in the form:

[ws][-]{ d | [d.]hh:mm[:ss[.ff]] }[ws]

Elements in square brackets ([ and ]) are optional. One selection from the list of alternatives enclosed in braces ({ and }) and separated by vertical bars (|) is required. The following table describes each element.

Element

Description

ws

Optional white space.

-

An optional minus sign, which indicates a negative TimeSpan.

d

Days, ranging from 0 to 10675199.

.

A culture-sensitive symbol that separates days from hours. The invariant format uses a period (".") character.

hh

Hours, ranging from 0 to 23.

:

The culture-sensitive time separator symbol. The invariant format uses a colon (":") character.

mm

Minutes, ranging from 0 to 59.

ss

Optional seconds, ranging from 0 to 59.

.

A culture-sensitive symbol that separates seconds from fractions of a second. The invariant format uses a period (".") character.

ff

Optional fractional seconds, consisting of one to seven decimal digits.

The components of s must collectively specify a time interval that is greater than or equal to TimeSpan.MinValue and less than or equal to TimeSpan.MaxValue.

The Parse(String) method tries to parse s by using each of the culture-specific formats for the current culture.

对调用者的说明

When a time interval component in the string to be parsed contains more than seven digits, parsing operations in the .NET Framework 3.5 and earlier versions may behave differently from parsing operations in the .NET Framework 4. In some cases, parsing operations that succeed in the .NET Framework 3.5 and earlier versions may fail and throw an OverflowException in the .NET Framework 4. In other cases, parsing operations that throw a FormatException in the .NET Framework 3.5 and earlier versions may fail and throw an OverflowException in the .NET Framework 4. The following example illustrates both scenarios.


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


The following example uses the Parse method to convert each element in a string array to a TimeSpan value. It changes the current system culture to Croatian - Croatia ("hr-HR") and English - United States ("en-US") to illustrate how the current system culture affects the parsing operation.


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


.NET Framework

受以下版本支持:4.5.2、4.5.1、4.5、4、3.5、3.0、2.0、1.1、1.0

.NET Framework Client Profile

受以下版本支持:4、3.5 SP1

可移植类库

受以下版本支持:可移植类库

适用于 Windows 应用商店应用的 .NET

受以下版本支持:Windows 8

适用于 Windows Phone 应用的 .NET

受以下版本支持:Windows Phone 8、Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008(不支持服务器核心角色), Windows Server 2008 R2(支持带 SP1 或更高版本的服务器核心角色;不支持 Itanium)

.NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参阅.NET Framework 系统要求

社区附加资源

添加
显示:
© 2014 Microsoft