user17510
user17510

Reputation: 1549

Convert Dates in C#

Does anyone know of an easy (built in) way to convert a string like '20081231T130000' (ICalendar I think) to DateTime in C#? Or do I need to parse the string?

Upvotes: 2

Views: 2129

Answers (2)

Andy White
Andy White

Reputation: 88475

This seemed to work:

string dateTimeString = "20080115T115959";
string format = "yyyyMMddTHHmmss";
IFormatProvider us = new System.Globalization.CultureInfo("en-US", true);

DateTime dt = DateTime.ParseExact(dateTimeString, format, us);

Upvotes: 2

Marc Gravell
Marc Gravell

Reputation: 1064044

Try this:

DateTime when = DateTime.ParseExact("20081231T130000",
    "yyyyMMddTHHmmss",CultureInfo.InvariantCulture);

Aside: it is close to the format used in xml, but not quite close enough - otherwise XmlConvert wraps this:

DateTime when = XmlConvert.ToDateTime("2008-12-31T13:00:00");

Upvotes: 9

Related Questions