Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a double value to a DateTime in c#?

I have the value 40880.051388 and am storing it as a double, if I open Excel and paste in a cell and apply the following custom format "m/d/yyyy h:mm" to that cell, I get "12/3/2011 1:14"

How can I do this parsing/Conversion in C#? I don't know if the value is milliseconds from a certain checkpoint, like epoch time, or if the value is in some specific prepared format, but how does excel come up with this particular value? Can it be done in C#?

I've tried working with TimeSpan, DateTime, and other like things in Visual Studio but am not getting anywhere.

like image 798
fifamaniac04 Avatar asked Dec 17 '12 18:12

fifamaniac04


2 Answers

Looks like you're using the old OLE Automation date. Use

DateTime.FromOADate(myDouble)
like image 56
Jaime Torres Avatar answered Oct 15 '22 21:10

Jaime Torres


Try something like this:-

double d = 40880.051388 ;
DateTime dt = DateTime.FromOADate(d);
like image 31
Rahul Tripathi Avatar answered Oct 15 '22 21:10

Rahul Tripathi