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

semantic_c0d3r picture semantic_c0d3r · May 4, 2012 · Viewed 12k times · Source

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!

Answer

Codo picture Codo · May 4, 2012

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.