代码之家  ›  专栏  ›  技术社区  ›  GilliVilla

Excel十进制格式到C#日期时间

  •  3
  • GilliVilla  · 技术社区  · 14 年前

    20090701 DateTime . 最好的方法是什么?

    3 回复  |  直到 14 年前
        1
  •  4
  •   LBushkin    14 年前
    DateTime.ParseExact( value.ToString(), "yyyymmdd" );
    

    这个 ParseExact 方法允许您为转换的日期/时间指定格式字符串。在你的例子中:一个四位数的年,然后两位数的月,然后两位数的月日。

        2
  •  0
  •   Biff MaGriff    14 年前

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

    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
  •  0
  •   ChaosPandion    14 年前

    一个不那么直观的答案。

    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);