• 欢迎访问我爱CSharp学习网,这里有最新最全的C#书籍,C#视频。
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏我爱C#学习网吧
  • 推荐使用最新版Chrome浏览器和火狐浏览器访问本网站

C# FromOADate and Excel Dates

C#杂烩 52csharp 250次浏览 0个评论 扫描二维码

FromOADate. Excel interop dates are stored as integers. It is possible to convert them to DateTimes. We use FromOADate to convert OLE Automation Dates to real DateTime values. We examine the FromOADate method.


Input OLE date:

39456

39482

39484

36526

36557

39270


Output DateTime:

1/9/2008

2/4/2008

2/6/2008

1/1/2000

2/1/2000

7/7/2007



Example. First, you can use FromOADate as a static method on the DateTime type. This method receives one double value as its parameter, and the returns a full DateTime. This will convert those five-digit integers to the regular DateTimes.



  1. using System;

  2. class Program

  3. {

  4.     static void Main()

  5.     {

  6.         var arr = new string[]

  7.         {

  8.             // OA Dates in early 2008.

  9.             “39456”, // (1/9/2008)

  10.             “39482”,

  11.             “39484”,

  12.             “39493”,

  13.             “39496”,

  14.             “39497”,

  15.             “39501”,

  16.             “39506”,

  17.             “39510”,

  18.             “39513”,

  19.             “39515”,

  20.             “39521”, // (3/14/2008)

  21.             // Different series starts here.

  22.             “36526”, // (1/1/2000)

  23.             “36557”, // (2/1/2000)

  24.             “39270”  // (7//7/2007)

  25.         };


  26.         foreach (string b in arr)

  27.         {

  28.             // We must have a double to convert the OA date to a real date.

  29.             double d = double.Parse(b);


  30.             // Get the converted date from the OLE automation date.

  31.             DateTime conv = DateTime.FromOADate(d);


  32.             // Write to console.

  33.             Console.WriteLine(“{0} = {1}”,

  34.                 b,

  35.                 conv.ToShortDateString());

  36.         }

  37.     }

  38. }


Output

39456 = 1/9/2008

39482 = 2/4/2008

39484 = 2/6/2008

39493 = 2/15/2008

39496 = 2/18/2008

39497 = 2/19/2008

39501 = 2/23/2008

39506 = 2/28/2008

39510 = 3/3/2008

39513 = 3/6/2008

39515 = 3/8/2008

39521 = 3/14/2008

36526 = 1/1/2000

36557 = 2/1/2000

39270 = 7/7/2007


https://www.dotnetperls.com/fromoadate

https://msdn.microsoft.com/ZH-CN/library/system.datetime.fromoadate(v=vs.110).aspx



我爱CSharp学习网 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明C# FromOADate and Excel Dates
喜欢 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址