如何使用 javascript 将 OLE 自动化日期转换为可读格式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10443325/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 09:54:49  来源:igfitidea点击:

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

javascriptdateole

提问by semantic_c0d3r

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?

您必须了解 .NET 方法“DateTime.FromOADate(double d)”。我需要在 javascript 中实现相同的功能。即给出一个像“40967.6424503935”这样的双值,它必须转换为“2/28/2012 3:25:07 PM”有人可以帮我吗?

Thanks in advance!

提前致谢!

回答by Codo

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:

自动化日期是自 1900 年 1 月 1 日以来的天数(奇怪的是,1900 年被视为闰年)。所以转换是:

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.

此解决方案创建一个 UTC 日期。当您显示它时,它将以您的本地时区显示。根据您的日期是本地日期还是 UTC 日期,这是正确的还是需要一些额外的时区摆弄。