views:

85

answers:

4

Hi,

I have a data string 'yyyymmddhhmmss' example: '20101001151014', how Do I parse this to date in C#?

A: 

DateTime.ParseExact you can use and parse it

  DateTime dt = DateTime.ParseExact("20101001151014", "yyyyMMddHHmmss", CultureInfo.InvariantCulture);
anishmarokey
@anishmarokey Its DateTime.ParseExact
Raj
Thanks Raj Its corrected
anishmarokey
+7  A: 
DateTime when = DateTime.ParseExact("20101001151014", "yyyyMMddHHmmss",
    CultureInfo.InvariantCulture);

Points to note: 24-hour hour is HH; 2-digit month is MM

Marc Gravell
A: 
public DateTime ShortDateStringToDate(string dateText)
{
    if (dateText == null)
        return DateTime.MinValue;

    if (dateText.Length != 14)
        throw new ArgumentException();

    string dateFormatString = "yyyyMMddHHmmss";
    return DateTime.ParseExact(dateText, dateFormatString, CultureInfo.InvariantCulture);
}
Yogesh
+2  A: 

Use the DateTime.ParseExact method.

Raj