Convert 20121004 (yyyyMMdd) to a valid date time?

Just use the DateTime.ParseExact method:

string date = "20121004";

string result = DateTime.ParseExact(date, "yyyyMMdd",
                CultureInfo.InvariantCulture).ToString("yyyy-MM-dd");

This also provides the advantage of validating the date before reformatting it with the hyphens. ParseExact throws an exception you can catch, if the date is not in valid range, or the format does not match.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)