Mono Class Library: System.DateTime Overview | Members

System.DateTime.ParseExact Method

Converts the specified string representation of a date and time to its DateTime equivalent using a specified format and IFormatProvider .

public static DateTime ParseExact (string s, string format, IFormatProvider provider)

Parameters

s
A string containing a date and time to convert. The format of the string is required to match the specified format exactly.
format
A string containing the expected format of s.
Note: For a list of valid format values, see System.Globalization.DateTimeFormatInfo .
provider
A IFormatProvider that supplies a System.Globalization.DateTimeFormatInfo object containing culture-specific format information about s .

Returns

A DateTime equivalent to the date and time contained in s.

Exceptions

TypeReason
ArgumentNullExceptions or format is a null reference.
FormatException

s or format is an empty string.

-or-

s does not contain a date and time that were recognized as the pattern specified in format .

Remarks

DateTime.ParseExact(string, string, IFormatProvider) constructs a DateTime from the string s. The string is required to specify a date and, optionally, a time in the specified format.

The string s is parsed using the culture-specific formatting information from the System.Globalization.DateTimeFormatInfo instance supplied by provider. If provider is null or a System.Globalization.DateTimeFormatInfo cannot be obtained from provider, the formatting information for the current system culture is used.

If the s string contains only a time, and no date, then the current date (DateTime.Now ) is used. If the string contains only a date and no time, this method assumes 12 a.m.

Leading, trailing, and inner white space characters are not allowed.

Note: For information on formatting system-supplied data types, see the IFormattable interface.

Example

This example demonstrates the DateTime.ParseExact(string, string, IFormatProvider) method.

C# Example

using System;
using System.Globalization;

public class DateTimeTest {
 public static void Main() {
     DateTimeFormatInfo dtfi = new DateTimeFormatInfo();

     DateTime dt = DateTime.ParseExact("January 22", dtfi.MonthDayPattern, null);
     Console.WriteLine(dt);
 }
}
   

The output is

1/22/2001 12:00:00 AM

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0