如何转换为Excel序列日期编号为.NET日期时间? [英] How do I convert an Excel serial date number to a .NET DateTime?
问题描述
我如何从Excel的序列日期转换为.NET日期和时间?
How do I convert from excel serial date to a .NET date time?
例如39938为2009/05/05。
For example 39938 is 05/05/2009.
推荐答案
其中39938是天数,因为1/1/1900?
Where 39938 is the number of days since 1/1/1900?
在这种情况下,使用此函数(C#):
In that case, use this function (C#):
public static DateTime FromExcelSerialDate(int SerialDate)
{
if (SerialDate > 59) SerialDate -= 1; //Excel/Lotus 2/29/1900 bug
return new DateTime(1899, 12, 31).AddDays(SerialDate);
}
VB:
Public Shared Function FromExcelSerialDate(ByVal SerialDate As Integer) As DateTime
If SerialDate > 59 Then SerialDate -= 1 ''// Excel/Lotus 2/29/1900 bug
Return New DateTime(1899, 12, 31).AddDays(SerialDate)
End Function
[更新]:
嗯...这方面的一个快速测试显示,它实际上两天假。不知道在哪里的差异。
[Update]:
Hmm... A quick test of that shows it's actually two days off. Not sure where the difference is.
好了:现在问题解决。详情请参考意见。尤其是看到推荐使用<一个href="http://msdn.microsoft.com/en-us/library/system.datetime.fromoadate.aspx"><$c$c>DateTime.FromOADate()$c$c>代替。
Okay: problem fixed now. See the comments for details. Especially see the recommendation to use DateTime.FromOADate()
instead.
这篇关于如何转换为Excel序列日期编号为.NET日期时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!