如何转换为Excel序列日期编号为.NET日期时间?日期、转换为、序列、编号

由网友(花落微凉梦清幽)分享简介:我如何从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是天数,因...

我如何从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.

好了:现在问题解决。详情请参考意见。尤其是看到推荐使用DateTime.FromOADate()代替。

Okay: problem fixed now. See the comments for details. Especially see the recommendation to use DateTime.FromOADate() instead.

阅读全文

相关推荐

最新文章