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;
- 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.
A DateTime equivalent to the date and time contained in s as specified by format, provider, and style.
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.
|
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.
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 );
DateTime Members DateTime.ParseExact Overload List Parse String CultureInfo CurrentInfo