TimeSpanConverter Class

Definition

Provides a type converter to convert TimeSpan objects to and from other representations.

public class TimeSpanConverter : System.ComponentModel.TypeConverter
Inheritance
TimeSpanConverter

Inherited Members

System.ComponentModel.TypeConverter

System.Object

Examples

The following code example converts a variable of type TimeSpan to a string, and vice versa. Both variables are represented in Ticks format.

TimeSpan ts(133333330);
String^ myTSStr = "5000000";
Console::WriteLine( TypeDescriptor::GetConverter( ts )->ConvertTo( ts, String::typeid ) );
Console::WriteLine( TypeDescriptor::GetConverter( ts )->ConvertFrom( myTSStr ) );
TimeSpan ts=new TimeSpan(133333330);
string myTSStr = "5000000";
Console.WriteLine(TypeDescriptor.GetConverter(ts).ConvertTo(ts, typeof(string))); 
Console.WriteLine(TypeDescriptor.GetConverter(ts).ConvertFrom(myTSStr));    
Dim ts As New TimeSpan(133333330)
Dim myTSStr As String = "5000000"
Console.WriteLine(TypeDescriptor.GetConverter(ts).ConvertTo(ts, GetType(String)))
Console.WriteLine(TypeDescriptor.GetConverter(ts).ConvertFrom(myTSStr))

Remarks

This converter can only convert a TimeSpan object to and from a string.

For more information about type converters, see the TypeConverter base class and How to: Implement a Type Converter.

Note

You should never create an instance of a TimeSpanConverter. Instead, call the GetConverter method of the TypeDescriptor class. For more information, see the examples in the TypeConverter base class.

Constructors

TimeSpanConverter()

Initializes a new instance of the TimeSpanConverter class.

Methods

CanConvertFrom(ITypeDescriptorContext, Type)

Gets a value indicating whether this converter can convert an object in the given source type to a TimeSpan using the specified context.

CanConvertTo(ITypeDescriptorContext, Type)

Gets a value indicating whether this converter can convert an object to the given destination type using the context.

ConvertFrom(ITypeDescriptorContext, CultureInfo, Object)

Converts the given object to a TimeSpan.

ConvertTo(ITypeDescriptorContext, CultureInfo, Object, Type)

Converts the given object to another type.