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

DateTime.Parse Method (String, IFormatProvider, DateTimeStyles)

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

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

public static DateTime Parse (
	string s,
	IFormatProvider provider,
	DateTimeStyles styles
)
public static DateTime Parse (
	String s, 
	IFormatProvider provider, 
	DateTimeStyles styles
)
public static function Parse (
	s : String, 
	provider : IFormatProvider, 
	styles : DateTimeStyles
) : DateTime

Parameters

s

A string containing a date and time to convert.

provider

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

styles

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 provider and styles.
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.

ArgumentException

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

The string s is parsed using the formatting information in a DateTimeFormatInfo supplied by provider.

This method attempts to ignore unrecognized data and parse s completely. 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:00 midnight is used. 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.

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.

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 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

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.