Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.
0 out of 1 rated this helpful - Rate this topic

DateTime.Parse Method (String, IFormatProvider)

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

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

public static DateTime Parse (
	string s,
	IFormatProvider provider
)
public static DateTime Parse (
	String s, 
	IFormatProvider provider
)
public static function Parse (
	s : String, 
	provider : IFormatProvider
) : DateTime
Not applicable.

Parameters

s

A string containing a date and time to convert.

provider

An IFormatProvider that supplies culture-specific format information about s.

Return Value

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

ArgumentNullException

s is a null reference (Nothing in Visual Basic).

FormatException

s does not contain a valid string representation of a date and time.

The string s is parsed using formatting information in the DateTimeFormatInfo object that is supplied either explicitly or implicitly by the provider parameter.

This method attempts to parse s completely and avoid throwing FormatException. It ignores unrecognized data if possible and fills in missing month, day, and year information with the current time. If s contains only a date and no time, this method assumes 12:00 midnight. Any leading, inner, or trailing white space character in s is ignored. The date and time can be bracketed with a pair of leading and trailing NUMBER SIGN characters ('#', U+0023), and can be trailed with one or more NULL characters (U+0000).

The s parameter must contain the representation of a date and time in one of the formats described in the DateTimeFormatInfo topic.

The provider parameter 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.

Because the DateTime.Parse(String,IFormatProvider) method tries to parse the string representation of a date and time using the formatting rules of the provider parameter, trying to parse a particular string across different cultures can fail. If a specific date and time format will be parsed across different locales, use one of the overloads of the ParseExact method and provide a format specifier.

Parsing Custom Cultures

If you parse a date and time string generated for a custom culture, use the ParseExact method instead of the Parse method to improve the probability that the parse operation will succeed. A custom culture date and time string can be complicated, and therefore difficult to parse. The Parse method attempts to parse a string with several implicit parse patterns, all of which might fail. The ParseExact method, in contrast, requires you to explicitly designate one or more exact parse patterns that are likely to succeed.

For more information about custom cultures, see the System.Globalization.CultureAndRegionInfoBuilder class.

Notes to Callers: Formatting is influenced by properties of the current DateTimeFormatInfo object, which is supplied by the provider parameter. One reason the Parse method can unexpectedly throw FormatException is if the current DateTimeFormatInfo.DateSeparator and DateTimeFormatInfo.TimeSeparator properties are set to the same value.

The following code example demonstrates the Parse 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 February 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 February 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 February 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 February 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 February 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 February 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 Server 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 Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

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

Community Additions

Show:
© 2014 Microsoft. All rights reserved.