asp.net.ph

DateTime.ParseExact Method ( String, String, IFormatProvider, DateTimeStyles )

System Namespace   DateTime Structure


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.

[ VB ]
<Serializable>
Overloads Public Shared Function ParseExact ( _
   ByVal s As String, _
   ByVal format As String, _
   ByVal provider As IFormatProvider, _
   ByVal style As DateTimeStyles _
) As DateTime

[ C# ]
[ Serializable ]
public static DateTime ParseExact (
   string s,
   string format,
   IFormatProvider provider,
   DateTimeStyles style
);

[ C++ ]
[ Serializable ]
public: static DateTime ParseExact (
   String* s,
   String* format,
   IFormatProvider* provider,
   DateTimeStyles style
);

[ JScript ]
public Serializable
static function ParseExact (
   s : String,
   format : String,
   provider : IFormatProvider,
   style : DateTimeStyles
) : DateTime;

Parameters

s
A string containing a date and time to convert.
format
The expected format of s .
provider
An IFormatProvider that supplies culture-specific formatting information about s.
style
The combination of one or more DateTimeStyles constants that indicate the permitted format of s.

Return Value

A DateTime equivalent to the date and time contained in s as specified by format, provider, and style.

Exceptions


Exception Type Condition
ArgumentNullException s or format is a null reference ( Nothing in Visual Basic ).
FormatException s or format is an empty string

-or-

s does not contain a date and time that corresponds to the pattern specified in format.


Remarks

This method throws FormatException if the format of s is not exactly as specified by the format pattern in format. If format consists of a single standard format character, the format pattern that character represents is used. For more information, see the DateTimeFormatInfo topic.

If s contains a time but no date, the style parameter determines whether the current date or a default date is used. If s contains a date but no time, 12 A.M. is used. The style parameter also determines whether leading, inner, or trailing white space characters are ignored.

Parameter provider supplies culture-specific date and time formatting information. For example, the names of the days of the week in a particular language, or the preferred presentation order of the month, day, and year. If provider is a null reference ( Nothing in Visual Basic ), the current culture is used.

Example

The below code snippet demonstrates using the ParseExact method.

[ 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   DateTime.ParseExact Overload List   Parse   String   CultureInfo   CurrentInfo 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