Note

Access to this page requires authorization. You can try signing in or .

Access to this page requires authorization. You can try .

TimeSpan.Parse Method

Definition

Namespace:
System
Assemblies:
netstandard.dll, System.Runtime.dll
Assembly:
System.Runtime.dll
Assembly:
netstandard.dll
Assemblies:
mscorlib.dll, System.Runtime.dll
Assembly:
mscorlib.dll

Important

Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.

Converts the string representation of a time interval to its TimeSpan equivalent.

Overloads

Name Description
Parse(ReadOnlySpan<Char>, IFormatProvider)

Converts the span representation of a time interval to its TimeSpan equivalent by using the specified culture-specific format information.

Parse(String, IFormatProvider)

Converts the string representation of a time interval to its TimeSpan equivalent by using the specified culture-specific format information.

Parse(String)

Converts the string representation of a time interval to its TimeSpan equivalent.

Parse(ReadOnlySpan<Char>, IFormatProvider)

Source:
TimeSpan.cs
Source:
TimeSpan.cs
Source:
TimeSpan.cs
Source:
TimeSpan.cs
Source:
TimeSpan.cs

Converts the span representation of a time interval to its TimeSpan equivalent by using the specified culture-specific format information.

public static TimeSpan Parse(ReadOnlySpan<char> input, IFormatProvider? formatProvider = default);
public static TimeSpan Parse(ReadOnlySpan<char> input, IFormatProvider formatProvider = default);
static member Parse : ReadOnlySpan<char> * IFormatProvider -> TimeSpan
Public Shared Function Parse (input As ReadOnlySpan(Of Char), Optional formatProvider As IFormatProvider = Nothing) As TimeSpan

Parameters

input
ReadOnlySpan<Char>

A span containing the characters that represent the time interval to convert.

formatProvider
IFormatProvider

An object that supplies culture-specific formatting information.

Returns

A time interval that corresponds to input, as specified by formatProvider.

Implements

Applies to

Parse(String, IFormatProvider)

Source:
TimeSpan.cs
Source:
TimeSpan.cs
Source:
TimeSpan.cs
Source:
TimeSpan.cs
Source:
TimeSpan.cs

Converts the string representation of a time interval to its TimeSpan equivalent by using the specified culture-specific format information.

public:
 static TimeSpan Parse(System::String ^ input, IFormatProvider ^ formatProvider);
public:
 static TimeSpan Parse(System::String ^ input, IFormatProvider ^ formatProvider) = IParsable<TimeSpan>::Parse;
public static TimeSpan Parse(string input, IFormatProvider formatProvider);
public static TimeSpan Parse(string input, IFormatProvider? formatProvider);
static member Parse : string * IFormatProvider -> TimeSpan
Public Shared Function Parse (input As String, formatProvider As IFormatProvider) As TimeSpan

Parameters

input
String

A string that specifies the time interval to convert.

formatProvider
IFormatProvider

An object that supplies culture-specific formatting information.

Returns

A time interval that corresponds to input, as specified by formatProvider.

Implements

Exceptions

input is null.

input has an invalid format.

input represents a number that is less than TimeSpan.MinValue or greater than TimeSpan.MaxValue.

-or-

At least one of the days, hours, minutes, or seconds components in input is outside its valid range.

Examples

The following example defines an array of CultureInfo objects, and uses each object in calls to 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;
using System.Globalization;
using System.Text.RegularExpressions;

public class Example2
{
 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)
 {
 try {
 TimeSpan ts = TimeSpan.Parse(value, culture);
 Console.Write("{0,20}", ts.ToString("c"));
 }
 catch (FormatException) {
 Console.Write("{0,20}", "Bad Format");
 }
 catch (OverflowException) {
 Console.Write("{0,20}", "Overflow");
 }
 }
 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 Bad Format 6.12:14:45.3448000
// 6:12:14:45,3448 Bad Format 6.12:14:45.3448000 Bad Format
// 6:34:14:45 Overflow Overflow Overflow
open System
open System.Globalization
open System.Text.RegularExpressions

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",-17}"""
for culture in cultures do
 header <- header +
 if culture.Equals CultureInfo.InvariantCulture then 
 $"""{"Invariant",20}"""
 else
 $"{culture.Name,20}"
printfn $"{header}\m"

for value in values do
 printf $"{value,-17}"
 for culture in cultures do
 try
 let ts = TimeSpan.Parse(value, culture)
 printf $"{ts,20:c}"
 with
 | :? FormatException ->
 printf $"""{"Bad Format",20}"""
 | :? OverflowException ->
 printf $"""{"Overflow",20}"""
 printfn "" 
// 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 Bad Format 6.12:14:45.3448000
// 6:12:14:45,3448 Bad Format 6.12:14:45.3448000 Bad Format
// 6:34:14:45 Overflow Overflow Overflow
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
 Try
 Dim ts As TimeSpan = TimeSpan.Parse(value, culture)
 Console.Write("{0,20}", ts.ToString("c"))
 Catch e As FormatException
 Console.Write("{0,20}", "Bad Format")
 Catch e As OverflowException
 Console.Write("{0,20}", "Overflow")
 End Try
 Next
 Console.WriteLine()
Next

' 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 Bad Format 6.12:14:45.3448000
' 6:12:14:45,3448 Bad Format 6.12:14:45.3448000 Bad Format
' 6:34:14:45 Overflow Overflow Overflow

Remarks

This method tries to parse input by using each of the culture-specific formats for the culture specified by formatProvider.

The formatProvider parameter is an IFormatProvider implementation that provides culture-specific information about the format of the returned string. The formatProvider parameter can be any of the following:

If formatProvider is null, the DateTimeFormatInfo object that is associated with the current culture is used.

The input string to the Parse methods 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.

If the input string is not a day value only, it must include an hours and a minutes component; other components are optional. If they are present, the values of each time component must fall within a specified range. For example, the value of hh, the hours component, must be between 0 and 23. Because of this, passing "23:00:00" to the Parse method returns a time interval of 23 hours. On the other hand, passing "24:00:00" returns a time interval of 24 days. Because "24" is outside the range of the hours component, it is interpreted as the days component.

The components of the input string 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 the input string by using each of the culture-specific formats for the current culture.

Applies to

Parse(String)

Source:
TimeSpan.cs
Source:
TimeSpan.cs
Source:
TimeSpan.cs
Source:
TimeSpan.cs
Source:
TimeSpan.cs

Converts the string representation of a time interval to its TimeSpan equivalent.

public:
 static TimeSpan Parse(System::String ^ s);
public static TimeSpan Parse(string s);
static member Parse : string -> TimeSpan
Public Shared Function Parse (s As String) As TimeSpan

Parameters

s
String

A string that specifies the time interval to convert.

Returns

A time interval that corresponds to s.

Exceptions

s has an invalid format.

s represents a number that is less than TimeSpan.MinValue or greater than TimeSpan.MaxValue.

-or-

At least one of the days, hours, minutes, or seconds components is outside its valid range.

Examples

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 Example1
{
 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
open System
open System.Globalization
open System.Threading

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 cultureNames = [| "hr-HR"; "en-US" |]

// Change the current culture.
for cultureName in cultureNames do
 Thread.CurrentThread.CurrentCulture <- CultureInfo cultureName
 printfn $"Current Culture: {Thread.CurrentThread.CurrentCulture.Name}" 
 for value in values do
 try 
 let ts = TimeSpan.Parse value
 printfn $"{value} --> {ts:c}"
 with 
 | :? FormatException ->
 printfn $"{value}: Bad Format"
 | :? OverflowException ->
 printfn $"{value}: Overflow"
 printfn "" 
// 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
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 cultureNames() As String = {"hr-HR", "en-US"}

' Change the current culture.
For Each cultureName As String In cultureNames
 Thread.CurrentThread.CurrentCulture = New CultureInfo(cultureName)
 Console.WriteLine("Current Culture: {0}",
 Thread.CurrentThread.CurrentCulture.Name)
 For Each value As String In values
 Try
 Dim ts As TimeSpan = TimeSpan.Parse(value)
 Console.WriteLine("{0} --> {1}", value, ts.ToString("c"))
 Catch e As FormatException
 Console.WriteLine("{0}: Bad Format", value)
 Catch e As OverflowException
 Console.WriteLine("{0}: Overflow", value)
 End Try
 Next
 Console.WriteLine()
Next

' 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

Remarks

The input string to the Parse methods 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.

If the input string is not a day value only, it must include an hours and a minutes component; other components are optional. If they are present, the values of each time component must fall within a specified range. For example, the value of hh, the hours component, must be between 0 and 23. Because of this, passing "23:00:00" to the Parse method returns a time interval of 23 hours. On the other hand, passing "24:00:00" returns a time interval of 24 days. Because "24" is outside the range of the hours component, it is interpreted as the days component.

The components of the input string 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 the input string by using each of the culture-specific formats for the current culture.

Applies to


Feedback

Was this page helpful?