Export (0) Print
Expand All
0 out of 1 rated this helpful - Rate this topic

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

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.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

public static DateTime ParseExact (
	string s,
	string format,
	IFormatProvider provider,
	DateTimeStyles style
)
public static DateTime ParseExact (
	String s, 
	String format, 
	IFormatProvider provider, 
	DateTimeStyles style
)
public 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

A bitwise combination of DateTimeStyles values that indicates the permitted format of s. A typical value to specify is None.

Return Value

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

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.

ArgumentException

style contains an invalid combination of DateTimeStyles values. For example, both AssumeLocal and AssumeUniversal.

The s parameter contains the date and time to parse. If the s parameter contains only a time and no date, the style parameter determines whether the current date or a default date is used. If the s parameter contains only a date and no time, midnight (00:00:00) is used. The style parameter also determines whether the s parameter can contain leading, inner, or trailing white space characters.

The format parameter contains a pattern that corresponds to the expected format of the s parameter. The pattern in the format parameter consists of one or more custom format specifiers from the Custom DateTime Format Strings table, or a single standard format specifier, which identifies a predefined pattern, from the Standard DateTime Format Strings table.

If you do not use date or time separators in a custom format pattern, use the invariant culture for the provider parameter and the widest form of each custom format specifier. For example, if you want to specify hours in the pattern, specify the wider form, "HH", instead of the narrower form, "H".

The provider parameter supplies culture-specific date and time formatting information, such as the names of the days of the week in a particular language, or the preferred presentation order of the month, day, and year. The format parameter is typically a culture represented by a CultureInfo object. If provider is a null reference (Nothing in Visual Basic), the current culture is used.

The following code example demonstrates the ParseExact method.

using System;
using System.Globalization;

namespace Parse
{
    class Class1
    {
        public static void Main(string[] args)
        {
// Assume the current culture is en-US. 
// The date is Feburary 16, 1992, 12 hours, 15 minutes and 12 seconds.

        string myDateTimeValue = "2/16/1992 12:15:12";
        DateTime myDateTime = DateTime.Parse(myDateTimeValue);
        Console.WriteLine("1) myDateTime       = {0}", myDateTime);

// Reverse month and day to conform to a different culture.
// The date is Feburary 16, 1992, 12 hours, 15 minutes and 12 seconds.

        IFormatProvider culture = new CultureInfo("fr-FR", true);
        string myDateTimeFrenchValue = "    16/02/1992 12:15:12";
        DateTime myDateTimeFrench =
            DateTime.Parse(myDateTimeFrenchValue,
                           culture,
                           DateTimeStyles.NoCurrentDateDefault);
        Console.WriteLine("2) myDateTimeFrench = {0}", myDateTimeFrench);
    
// The date is Feburary 16, 1992, 12 hours, 15 minutes and 12 seconds.

        string[] expectedFormats = {"G", "g", "f" ,"F"};
        myDateTimeFrench = 
                DateTime.ParseExact(myDateTimeFrenchValue,
                                    expectedFormats,
                                    culture,
                                    DateTimeStyles.AllowWhiteSpaces);
        Console.WriteLine("3) myDateTimeFrench = {0}", myDateTimeFrench);
        }
    }
}
/*
This example yields the following results:

1) myDateTime       = 2/16/1992 12:15:12 PM
2) myDateTimeFrench = 2/16/1992 12:15:12 PM
3) myDateTimeFrench = 2/16/1992 12:15:12 PM
*/

package Parse; 

import System.*;
import System.Globalization.*;

class Class1
{
    public static void main(String[] args)
    {
        // Assume the current culture is en-US. 
        // The date is Feburary 16, 1992, 12 hours, 15 minutes and 12 seconds.
        String myDateTimeValue = "2/16/1992 12:15:12";
        DateTime myDateTime = DateTime.Parse(myDateTimeValue);
        Console.WriteLine("1) myDateTime       = {0}", myDateTime);
        // Reverse month and day to conform to a different culture.
        // The date is Feburary 16, 1992, 12 hours, 15 minutes and 12 seconds.
        IFormatProvider culture = new CultureInfo("fr-FR", true);
        String myDateTimeFrenchValue = "    16/02/1992 12:15:12";
        DateTime myDateTimeFrench = DateTime.Parse(myDateTimeFrenchValue, 
            culture, DateTimeStyles.NoCurrentDateDefault);
        Console.WriteLine("2) myDateTimeFrench = {0}", myDateTimeFrench);
        // The date is Feburary 16, 1992, 12 hours, 15 minutes and 12 seconds.
        String expectedFormats[] =  { "G", "g", "f", "F" };
        myDateTimeFrench = DateTime.ParseExact(myDateTimeFrenchValue, 
            expectedFormats, culture, DateTimeStyles.AllowWhiteSpaces);
        Console.WriteLine("3) myDateTimeFrench = {0}", myDateTimeFrench);
    } //main
} //Class1
/*
This example yields the following results:

1) myDateTime       = 2/16/1992 12:15:12 PM
2) myDateTimeFrench = 2/16/1992 12:15:12 PM
3) myDateTimeFrench = 2/16/1992 12:15:12 PM
*/

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.