Friday, February 1, 2008

Converting Excel dates (numeric) to regular dates

When importing dates from Excel, it is common to obtain a number instead of a date. That is the number of days after 1/1/1900.

private static string ConvertExcelDate(string strDate)
{
int iDate = 0;
if (! int.TryParse(strDate, out iDate))
return "";
DateTime dtDate = new DateTime(1900, 1, 1);
dtDate = dtDate.AddDays(iDate - 2);
return dtDate.ToShortDateString();
}

No comments: