Parsing Integer Value As Datetime

If your CurrentCulture supports yyyyMMdd format as a standard date and time format, you can just use DateTime.Parse method like;

int i = 20140820;
DateTime dt = DateTime.Parse(i.ToString());

If it doesn’t support, you need to use DateTime.ParseExact or DateTime.TryParseExact methods to parse it as custom date and time format.

int i = 20140820;
DateTime dt;
if(DateTime.TryParseExact(i.ToString(), "yyyyMMdd", 
                          CultureInfo.InvariantCulture,
                          DateTimeStyles.None, out dt))
{
    Console.WriteLine(dt);
}

Then you can format your DateTime with .ToString() method like;

string formattedDateTime = dt.ToString("yyyy.MM.dd", CultureInfo.InvariantCulture);

Leave a Comment

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