Excel date conversion in .NET

R

RW

When I import an excel date column into my .NET apllication I get a number
instead of a date. When I look at the excel file and examine the cell format,
it is set to date, I dont know what is going on here. Some cells are set to
general format and they come out fine, I only have a problem with the ones
set to date.
 
D

DBius

When I import an excel date column into my .NET apllication I get a number
instead of a date. When I look at the excel file and examine the cell format,
it is set to date, I dont know what is going on here. Some cells are set to
general format and they come out fine, I only have a problem with the ones
set to date.

Good Morning RW,

I have been having the same problem as you have and after two weeks of
searching I have found a solution on the http://www.CodeProject.com/.
Here is the article you would like to reference: (
http://www.codeproject.com/datetime/exceldmy.asp?df=100&forumid=4548&exp=0&select=258452#xx258452xx
)

My cells actually contained a date with time which in excel format
stores it as a double I may have taken the hard way but it works. My
sample:

string strLocalDate =
(((Excel.Range)workSheet.Cells[rowIndex,
colIndex0]).Value2.ToString());

double doubleLocalDate =
Convert.ToDouble(strLocalDate);
int intLocalDate =
Convert.ToInt32(doubleLocalDate);
int strDD;
int strMM;
int strYYYY;

int l = intLocalDate + 68569 + 2415019;
int n = (( 4 * l ) / 146097);
l = l - (( 146097 * n + 3 ) / 4);
int i = (( 4000 * ( l + 1 ) ) / 1461001);
l = l - (( 1461 * i ) / 4) + 31;
int j = (( 80 * l ) / 2447);
strDD = l - (( 2447 * j ) / 80);
l = (j / 11);
strMM = j + 2 - ( 12 * l );
strYYYY = 100 * ( n - 49 ) + i + l;

Console.WriteLine(strDD + "/" + strMM + "/" +
strYYYY);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top