javascriptdateole

How to convert OLE Automation Date to readable format using javascript?


You must be aware of the .NET method "DateTime.FromOADate(double d)". I need to implement the same functionality in javascript. i.e given a double value like "40967.6424503935" it has to be converted to "2/28/2012 3:25:07 PM" Can someone please help me out?

Thanks in advance!


Solution

  • The automation date is the number of days since January 1, 1900 (with the year 1900 strangely being treated as a leap year). So a conversion is:

    var oaDate = 40967.6424503935;
    var date = new Date();
    date.setTime((oaDate - 25569) * 24 * 3600 * 1000);
    alert(date);
    

    This solution creates a UTC date. When you display it, it'll be displayed in your local timezone. Depending on whether your date is a local date or a UTC date, this is correct or will require some additional timezone fiddling.