Export (0) Print
Expand All

DateTimeConverter Class

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

For a list of all members of this type, see DateTimeConverter Members.

System.Object
   System.ComponentModel.TypeConverter
      System.ComponentModel.DateTimeConverter

[Visual Basic]
Public Class DateTimeConverter
   Inherits TypeConverter
[C#]
public class DateTimeConverter : TypeConverter
[C++]
public __gc class DateTimeConverter : public TypeConverter
[JScript]
public class DateTimeConverter extends TypeConverter

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Remarks

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

ConvertFrom uses the Parse method of DateTime to convert from a string.

ConvertTo uses the current culture, if a CultureInfo object is not supplied. Generally, ConvertTo uses ShortDatePattern to format a date and ShortDatePattern with ShortTimePattern to format a date and time. If InvariantCulture is passed, ConvertTo uses yyyy-mm-dd to format a date and ToString to format a date and time.

For more information about type converters, see the TypeConverter base class and Implementing a Type Converter.

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

Example

[Visual Basic, C#, C++] The following sample converts a variable of type DateTime to a string, and vice versa.

[Visual Basic] 
Dim dt As New DateTime(1990, 5, 6)
Console.WriteLine(TypeDescriptor.GetConverter(dt).ConvertTo(dt, GetType(String)))
Dim myStr As String = "1991-10-10"
Console.WriteLine(TypeDescriptor.GetConverter(dt).ConvertFrom(myStr))

[C#] 
DateTime dt=new DateTime(1990,5,6);
Console.WriteLine(TypeDescriptor.GetConverter(dt).ConvertTo(dt, typeof(string)));
string myStr="1991-10-10";
Console.WriteLine(TypeDescriptor.GetConverter(dt).ConvertFrom(myStr));

[C++] 
DateTime dt(1990,5,6);
Console::WriteLine(TypeDescriptor::GetConverter(__box(dt))->ConvertTo(__box(dt), __typeof(String)));
String* myStr = S"1991-10-10";
Console::WriteLine(TypeDescriptor::GetConverter(__box(dt))->ConvertFrom(myStr));

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.ComponentModel

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

Assembly: System (in System.dll)

See Also

DateTimeConverter Members | System.ComponentModel Namespace

Show:
© 2014 Microsoft