【问题标题】:Excel decimal format to C# DateTimeExcel 十进制格式到 C# DateTime
【发布时间】:2010-07-13 22:07:21
【问题描述】:

在我的 C# 数据访问层中...我正在从 Excel 中检索数据集...并且有一个十进制 excel 字段以以下格式返回日期:20090701。我需要将其转换为 C#DateTime。最好的方法是什么?

【问题讨论】:

    标签: c# excel datetime


    【解决方案1】:
    DateTime.ParseExact( value.ToString(), "yyyymmdd" );
    

    ParseExact 方法允许您为要转换的日期/时间指定格式字符串。在您的情况下:四位数的年份,然后是两位数的月份,然后是两位数的月份日期。

    【讨论】:

      【解决方案2】:

      如果你想在应用程序范围内实现它,我会做这样的事情。

      System.Globalization.CultureInfo cultureInfo =
                  new System.Globalization.CultureInfo("en-CA");
              // Defining various date and time formats.
              dateTimeInfo.LongDatePattern = "yyyyMMdd";
              dateTimeInfo.ShortDatePattern = "yyyyMMdd";
              dateTimeInfo.FullDateTimePattern = "yyyyMMdd";
              // Setting application wide date time format.
              cultureInfo.DateTimeFormat = dateTimeInfo;
      
      
          // Assigning our custom Culture to the application.
          //Application.CurrentCulture = cultureInfo;
          Thread.CurrentThread.CurrentCulture = cultureInfo;
          Thread.CurrentThread.CurrentUICulture = cultureInfo;
      
      DateTime.Parse(excelDate);
      

      【讨论】:

        【解决方案3】:

        还有一个不太直观的好衡量标准。

        var a = 20090701m;
        var b = a / 10000;
        var year = (int)b;
        var c = (b - year) * 100;
        var month = (int)c;
        var day = (int)((c - month) * 100);
        var dt = new DateTime(year, month, day);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-07-21
          • 1970-01-01
          • 1970-01-01
          • 2010-10-15
          • 2010-12-09
          相关资源
          最近更新 更多