Converts the string representation of a time interval to its TimeSpan equivalent by using the specified culture-specific formatting information, and returns a value that indicates whether the conversion succeeded.
public:
static bool TryParse(System::String ^ input, IFormatProvider ^ formatProvider, [Runtime::InteropServices::Out] TimeSpan % result);
public:
static bool TryParse(System::String ^ input, IFormatProvider ^ formatProvider, [Runtime::InteropServices::Out] TimeSpan % result) = IParsable<TimeSpan>::TryParse;
public static bool TryParse(string input, IFormatProvider formatProvider, out TimeSpan result);
public static bool TryParse(string? input, IFormatProvider? formatProvider, out TimeSpan result);
static member TryParse : string * IFormatProvider * TimeSpan -> bool
Public Shared Function TryParse (input As String, formatProvider As IFormatProvider, ByRef result As TimeSpan) As Boolean
Parameters
A string that specifies the time interval to convert.
An object that supplies culture-specific formatting information.
When this method returns, contains an object that represents the time interval specified by input
, or Zero if the conversion failed. This parameter is passed uninitialized.
true
if input
was converted successfully; otherwise, false
. This operation returns false
if the input
parameter is null
or Empty, has an invalid format, represents a time interval that is less than TimeSpan.MinValue or greater than TimeSpan.MaxValue, or has at least one days, hours, minutes, or seconds component outside its valid range.
The following example defines an array of CultureInfo objects, and uses each object in calls to the TryParse(String, IFormatProvider, TimeSpan) 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;
using System.Globalization;
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);
Console.WriteLine();
foreach (string value in values)
{
Console.Write("{0,-17}", value);
foreach (CultureInfo culture in cultures)
{
TimeSpan interval = new TimeSpan();
if (TimeSpan.TryParse(value, culture, out interval))
Console.Write("{0,20}", interval.ToString("c"));
else
Console.Write("{0,20}", "Unable to Parse");
}
Console.WriteLine();
}
}
}
// The example displays the following output:
// String en-US ru-RU Invariant
//
// 6 6.00:00:00 6.00:00:00 6.00:00:00
// 6:12 06:12:00 06:12:00 06:12:00
// 6:12:14 06:12:14 06: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 6.12:14:45 6.12:14:45 6.12:14:45
// 6:12:14:45.3448 6.12:14:45.3448000 Unable to Parse 6.12:14:45.3448000
// 6:12:14:45,3448 Unable to Parse 6.12:14:45.3448000 Unable to Parse
// 6:34:14:45 Unable to Parse Unable to Parse Unable to Parse
open System
open System.Globalization
let 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" |]
let cultures =
[| CultureInfo "en-US"
CultureInfo "ru-RU"
CultureInfo.InvariantCulture |]
let mutable header = String.Format("{0,-17}", "String")
for culture in cultures do
header <-
if culture.Equals CultureInfo.InvariantCulture then
String.Format("{0,20}", "Invariant")
else
String.Format("{0,20}", culture.Name)
printfn $"{header}\n"
for value in values do
printf $"{value,-17}"
for culture in cultures do
match TimeSpan.TryParse(value, culture) with
| true, interval ->
printfn $"{interval,20:c}"
| _ ->
printfn "%20s" "Unable to Parse"
// The example displays the following output:
// String en-US ru-RU Invariant
//
// 6 6.00:00:00 6.00:00:00 6.00:00:00
// 6:12 06:12:00 06:12:00 06:12:00
// 6:12:14 06:12:14 06: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 6.12:14:45 6.12:14:45 6.12:14:45
// 6:12:14:45.3448 6.12:14:45.3448000 Unable to Parse 6.12:14:45.3448000
// 6:12:14:45,3448 Unable to Parse 6.12:14:45.3448000 Unable to Parse
// 6:34:14:45 Unable to Parse Unable to Parse Unable to Parse
Imports System.Globalization
Module Example
Public Sub Main()
Dim values() As String = { "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" }
Dim cultures() As CultureInfo = { New CultureInfo("en-US"),
New CultureInfo("ru-RU"),
CultureInfo.InvariantCulture }
Dim header As String = String.Format("{0,-17}", "String")
For Each culture As CultureInfo In cultures
header += If(culture.Equals(CultureInfo.InvariantCulture),
String.Format("{0,20}", "Invariant"),
String.Format("{0,20}", culture.Name))
Next
Console.WriteLine(header)
Console.WriteLine()
For Each value As String In values
Console.Write("{0,-17}", value)
For Each culture As CultureInfo In cultures
Dim interval As New TimeSpan()
If TimeSpan.TryParse(value, culture, interval) Then
Console.Write("{0,20}", interval.ToString("c"))
Else
Console.Write("{0,20}", "Unable to Parse")
End If
Next
Console.WriteLine()
Next
End Sub
End Module
' The example displays the following output:
' String en-US ru-RU Invariant
'
' 6 6.00:00:00 6.00:00:00 6.00:00:00
' 6:12 06:12:00 06:12:00 06:12:00
' 6:12:14 06:12:14 06: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 6.12:14:45 6.12:14:45 6.12:14:45
' 6:12:14:45.3448 6.12:14:45.3448000 Unable to Parse 6.12:14:45.3448000
' 6:12:14:45,3448 Unable to Parse 6.12:14:45.3448000 Unable to Parse
' 6:34:14:45 Unable to Parse Unable to Parse Unable to Parse
Remarks
For more information about this API, see Supplemental API remarks for TimeSpan.TryParse.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4