ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Excel date conversion in .NET (https://www.excelbanter.com/excel-programming/400624-excel-date-conversion-net.html)

RW

Excel date conversion in .NET
 
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.

DBius

Excel date conversion in .NET
 
On Nov 5, 1:13 pm, RW wrote:
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/... 52#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);



All times are GMT +1. The time now is 09:00 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com