asp.net.ph

DateTime.ParseExact Method

System Namespace   DateTime Structure


Converts the specified string representation of a date and time to its DateTime equivalent. The format of the string representation must match a specified format exactly.

Overload List

1. Converts the specified string representation of a date and time to its DateTime equivalent using the specified format and culture-specific format information. The format of the string representation must match the specified format exactly.

2. Converts the specified string representation of a date and time to its DateTime equivalent using the specified format, culture-specific format information, and style. The format of the string representation must match the specified format exactly.

3. Converts the specified string representation of a date and time to its DateTime equivalent using the specified array of formats, culture-specific format information, and style. The format of the string representation must match at least one of the specified formats exactly.


Example

The below code snippet demonstrates using the ParseExact method.

NOTE: This example shows how to use one of the overloaded versions of ParseExact. For other examples that might be available, see the individual overload topics.

[ VB ]
Dim strMyDateTime As String
strMyDateTime = "2/16/1992 12:15:12"

' myDateTime gets Feburary 16, 1992, 12 hours, 15 min and 12 sec.
Dim myDateTime As System.DateTime
myDateTime = System.DateTime.Parse ( strMyDateTime )

Dim format As New System.Globalization.CultureInfo ( "fr-FR", True )

' Reverse month and day to conform to a different format.
Dim strMyDateTimeFrench As String
strMyDateTimeFrench = "    16/02/1992 12:15:12"

' myDateTimeFrench gets Feburary 16, 1992, 12 hours,
' 15 min and 12 sec.
Dim myDateTimeFrench As System.DateTime
myDateTimeFrench = System.DateTime.Parse ( _
                           strMyDateTimeFrench, _
                           format, _
                           System.Globalization. _
                              DateTimeStyles.NoCurrentDateDefault )

Dim expectedFormats As String ( ) = { "G", "g", "f", "F" }
' myDateTimeFrench gets Feburary 16, 1992, 12 hours,
' 15 min and 12 sec.
myDateTimeFrench = System.DateTime.ParseExact ( _
                              strMyDateTimeFrench, _
                              expectedFormats, _
                              format, _
                              System.Globalization. _
                                 DateTimeStyles.AllowWhiteSpaces )

[ C# ]
string strMyDateTime = "2/16/1992 12:15:12";

// myDateTime gets Feburary 16, 1992, 12 hours, 15 min and 12 sec.
System.DateTime myDateTime =
   System.DateTime.Parse ( strMyDateTime );

System.IFormatProvider format =
   new System.Globalization.CultureInfo ( "fr-FR", true );

// Reverse month and day to conform to a different format.
string strMyDateTimeFrench = "    16/02/1992 12:15:12";

// myDateTimeFrench gets Feburary 16, 1992, 12 hours,
// 15 min and 12 sec.
System.DateTime myDateTimeFrench =
   System.DateTime.Parse ( strMyDateTimeFrench,
                    format,
                    System.Globalization.
                     DateTimeStyles.NoCurrentDateDefault );

string [ ] expectedFormats = { "G", "g", "f" ,"F" };
// myDateTimeFrench gets Feburary 16, 1992, 12 hours,
// 15 min and 12 sec.
myDateTimeFrench =
   System.DateTime.ParseExact ( strMyDateTimeFrench,
                        expectedFormats,
                        format,
                        System.Globalization.
                        DateTimeStyles.AllowWhiteSpaces );
See Also

DateTime Members Skip Navigation Links




Home
Suggested Reading


Previous page Back to top Next page

© 2000-2010 Rey Nuñez All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note

You can help support asp.net.ph