Try to parse string("2008-03-01 10:00") to DateTime in CSharp

Description

The following code shows how to try to parse string("2008-03-01 10:00") to DateTime.

Example


using System;/* w w  w  .j a  v a2s.  c o  m*/
using System.Globalization;
public class MainClass{
  public static void Main(String[] argv){  
    string dateString;
    CultureInfo culture;
    DateTimeStyles styles;
    DateTime dateResult;
    
    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.





















Home »
  C# Tutorial »
    Data Types »




C# Data Types
Bool
Byte
Char
Decimal
Double
Float
Integer
Long
Short
String
C# Array
Array Example
Byte Array
C# Standard Data Type Format
BigInteger
Complex
Currency
DateTime
DateTimeOffset
DateTime Format Parse Convert
TimeSpan
TimeZone
Enum
Null
tuple
var