Excel date to datetime

  • Thread starter =?iso-8859-1?q?H=E5vard_Olerud_Eriksen?=
  • Start date
?

=?iso-8859-1?q?H=E5vard_Olerud_Eriksen?=

I've got a small app which reads dates from an Excel spreadsheet adding
them onto a listview. The problem is that the dates are read in as ints
(or possibly doubles, they look like ints). So for instance the date
03.07.2000 is represented like 36710, 04.07.2000 as 36711 etc. The
problem as you may understand is that I want them to be stored as dates
(or ideally as strings). I've tried all sorts of Convert.* functions
and casts but they all end up throwing exceptions.
The column is read like this:
Excel.Range range = worksheet.get_Range("A"+i.ToString(),
"A"+i.ToString()); and currently the list view is populated like this:
listView1.Items.Add(new ListViewItem(range.Cells.Value2.ToString()));

Does anyone have any hints, tips, code snippets or otherwise which can
help me solve my problem?

MTIA,
Håvard
 
G

Guest

Dates in Excel are based on 1/1/1900, so the # you are getting is the # of
days since then, thus, if you do something like:

TimeSpan dateFromExcel = new TimeSpan(38530,0,0,0);
DateTime resultingDate = new DateTime(1900,1,1).Add(dateFromExcel);
Console.WriteLine( resultingDate.ToString() );

In theory you should get the correct result... however I am getting a date
of 7/5/2000 (MM/DD/YYYY), constantly 2 days off.

I apologize for not being able to solve this for you completely, but this
will get you pretty close. I hope this helps.

Brendan
 

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