C# DateTime TryParse(String, IFormatProvider, DateTimeStyles, DateTime)
Description
DateTime TryParse(String, IFormatProvider, DateTimeStyles,
DateTime)
converts the specified string representation of a date
and time to its DateTime equivalent using the specified culture-specific
format information and formatting style, and returns a value that indicates
whether the conversion succeeded.
Syntax
DateTime.TryParse(String, IFormatProvider, DateTimeStyles, DateTime)
has the following syntax.
public static bool TryParse(
string s,// ww w . j a v a 2 s . c o m
IFormatProvider provider,
DateTimeStyles styles,
out DateTime result
)
Parameters
DateTime.TryParse(String, IFormatProvider, DateTimeStyles, DateTime)
has the following parameters.
s
- A string containing a date and time to convert.provider
- An object that supplies culture-specific formatting information about s.styles
- A bitwise combination of enumeration values that defines how to interpret the parsed date in relation to the current time zone or the current date. A typical value to specify is None.result
- When this method returns, contains the DateTime value equivalent to the date and time contained in s, if the conversion succeeded, or MinValue if the conversion failed. The conversion fails if the s parameter is null, is an empty string (""), or does not contain a valid string representation of a date and time. This parameter is passed uninitialized.
Returns
DateTime.TryParse(String, IFormatProvider, DateTimeStyles, DateTime)
method returns true if the s parameter was converted successfully; otherwise, false.
Example
The following example illustrates the DateTime.TryParse(String, IFormatProvider, DateTimeStyles, DateTime) method.
using System;//w ww. jav a2 s.com
using System.Globalization;
public class MainClass{
public static void Main(String[] argv){
string dateString;
CultureInfo culture;
DateTimeStyles styles;
DateTime dateResult;
// Parse a date and time with no styles.
dateString = "03/01/2009 10:00 AM";
culture = CultureInfo.CreateSpecificCulture("en-US");
styles = DateTimeStyles.None;
if (DateTime.TryParse(dateString, culture, styles, out dateResult))
System.Console.WriteLine("{0} converted to {1} {2}.",
dateString, dateResult, dateResult.Kind);
else
System.Console.WriteLine("Unable to convert {0} to a date and time.",
dateString);
// Parse the same date and time with the AssumeLocal style.
styles = DateTimeStyles.AssumeLocal;
if (DateTime.TryParse(dateString, culture, styles, out dateResult))
System.Console.WriteLine("{0} converted to {1} {2}.",
dateString, dateResult, dateResult.Kind);
else
System.Console.WriteLine("Unable to convert {0} to a date and time.", dateString);
dateString = "2009/03/01T10:00:00-5:00";
styles = DateTimeStyles.AssumeLocal;
if (DateTime.TryParse(dateString, culture, styles, out dateResult))
System.Console.WriteLine("{0} converted to {1} {2}.",
dateString, dateResult, dateResult.Kind);
else
System.Console.WriteLine("Unable to convert {0} to a date and time.", dateString);
// Attempt to convert a string in improper ISO 8601 format.
dateString = "03/01/2009T10:00:00-5:00";
if (DateTime.TryParse(dateString, culture, styles, out dateResult))
System.Console.WriteLine("{0} converted to {1} {2}.",
dateString, dateResult, dateResult.Kind);
else
System.Console.WriteLine("Unable to convert {0} to a date and time.", dateString);
dateString = "2008-03-01 10:00";
culture = CultureInfo.CreateSpecificCulture("fr-FR");
styles = DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeLocal;
if (DateTime.TryParse(dateString, culture, styles, out dateResult))
System.Console.WriteLine("{0} converted to {1} {2}.",
dateString, dateResult, dateResult.Kind);
else
System.Console.WriteLine("Unable to convert {0} to a date and time.", dateString);
}
}
The code above generates the following result.