Try to parse string to DateTime using culture-specific format information and formatting style in CSharp
Description
The following code shows how to try to parse string to DateTime using culture-specific format information and formatting style.
Example
/*from w w w . j a va 2 s.c om*/
using System;
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);
}
}
The code above generates the following result.