C# TimeSpan Parse(String, IFormatProvider)
Description
TimeSpan Parse(String, IFormatProvider)
converts
the string representation of a time interval to its TimeSpan equivalent
by using the specified culture-specific format information.
Syntax
TimeSpan.Parse(String, IFormatProvider)
has the following syntax.
public static TimeSpan Parse(
string input,
IFormatProvider formatProvider
)
Parameters
TimeSpan.Parse(String, IFormatProvider)
has the following parameters.
input
- A string that specifies the time interval to convert.formatProvider
- An object that supplies culture-specific formatting information.
Returns
TimeSpan.Parse(String, IFormatProvider)
method returns A time interval that corresponds to input, as specified by formatProvider.
Example
The following example calls the Parse(String, IFormatProvider) method to parse the elements in a string array.
The example illustrates how the conventions of a specific culture influence the formatting operation.
using System;/*from w w w.j a v a 2 s .c om*/
using System.Globalization;
using System.Text.RegularExpressions;
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" };
CultureInfo[] cultures = { new CultureInfo("en-US"),
new CultureInfo("ru-RU"),
CultureInfo.InvariantCulture };
string header = String.Format("{0,-17}", "String");
foreach (CultureInfo culture in cultures)
header += culture.Equals(CultureInfo.InvariantCulture) ?
String.Format("{0,20}", "Invariant") :
String.Format("{0,20}", culture.Name);
Console.WriteLine(header);
foreach (string value in values)
{
Console.Write("{0,-17}", value);
foreach (CultureInfo culture in cultures)
{
TimeSpan ts = TimeSpan.Parse(value, culture);
Console.WriteLine("{0,20}", ts.ToString("c"));
}
}
}
}
The code above generates the following result.