Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 557
semantic_c0d3r Avatar asked Dec 13 '22 02:12

semantic_c0d3r


1 Answers

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.

like image 100
Codo Avatar answered Jan 12 '23 00:01

Codo