如何将Excel序列号转换为.NET DateTime? [英] How do I convert an Excel serial date number to a .NET DateTime?

查看:122
本文介绍了如何将Excel序列号转换为.NET DateTime?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从Excel日期转换为.NET日期时间?

How do I convert from excel serial date to a .NET date time?

例如39938是05/05/2009。

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.

这篇关于如何将Excel序列号转换为.NET DateTime?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆