Search code examples
c#datetimeexcel-interopole-automation

Converting Timespan to DateTime in C#


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.


Solution

  • You could do the following

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