如何将 Excel 序列日期号转换为 .NET DateTime? [英] How do I convert an Excel serial date number to a .NET DateTime?
问题描述
如何将 excel 序列日期转换为 .NET 日期时间?
例如 39938 是 05/05/2009.
其中 39938 是自 1/1/1900 以来的天数?
在这种情况下,使用框架库函数DateTime.FromOADate()
.
此函数封装所有细节,并进行边界检查.>
就其历史价值而言,这是一个可能的实现:
(C#)
public static DateTime FromExcelSerialDate(int SerialDate){如果 (SerialDate > 59) SerialDate -= 1;//Excel/Lotus 2/29/1900 错误返回新日期时间(1899, 12, 31).AddDays(SerialDate);}
VB
公共共享函数 FromExcelSerialDate(ByVal SerialDate As Integer) As DateTime如果 SerialDate >59 然后 SerialDate -= 1 ' Excel/Lotus 2/29/1900 错误返回新日期时间(1899, 12, 31).AddDays(SerialDate)结束函数
<小时>
[更新]:
嗯...快速测试表明它实际上是两天休息.不知道区别在哪里.
好的:问题现在解决了.详情见评论.
How do I convert from excel serial date to a .NET date time?
For example 39938 is 05/05/2009.
Where 39938 is the number of days since 1/1/1900?
In that case, use the framework library function DateTime.FromOADate()
.
This function encapsulates all the specifics, and does bounds checking.
For its historical value, here is a possible implementation:
(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.
Okay: problem fixed now. See the comments for details.
这篇关于如何将 Excel 序列日期号转换为 .NET DateTime?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!