Converting Timespan to DateTime in C#

IrfanRaza picture IrfanRaza · Jan 25, 2011 · Viewed 40.4k times · Source

I am reading Excel worksheet data using C# and Microsoft.Office.Interop. The sheet contains some date values. When I am trying to read that value it is just giving the number (probably TimeSpan). I am having problem converting this number into DateTime.

Below is the code:

TimeSpan ts = TimeSpan.Parse(((Range)ws.Cells[4, 1]).Value2.ToString());

Where ws is Excel.WorkSheet.

Can anybody explain how should I convert this number (TimeSpan) into DateTime?

Thanks for sharing your valuable time.

Answer

Dimi Takis picture Dimi Takis · Jan 25, 2011

You could do the following

double d = double.Parse(((Range)ws.Cells[4, 1]).Value2.ToString());

DateTime conv = DateTime.FromOADate(d);