我有一个电子表格是Excel的日期对象,但变为双(像39820.0为2009年1月7日)出来时C1的XLS类的细胞。我看这是一个Julian日期格式。谁能告诉我如何分析它放回一个DateTime在C#?

更新:它看起来像我可能没有一个Julian日期,而是自1899年12月30日的天数

有帮助吗?

解决方案

我认为Excel中只是使用标准OLE自动化 DATE 类型可将其转化与DateTime.FromOADate方法。

的代码块,

using System;

namespace DateFromDouble
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(DateTime.FromOADate(39820.0));
        }
    }
}

输出:

1/7/2009 12:00:00 AM

其他提示

有一个在一个System.Globalization类JulianCalendar;这里是你将如何使用它:

            JulianCalendar c = new JulianCalendar();
            DateTime time = c.ToDateTime(2009, 1, 7, 0, 0, 0, 0);
            Console.WriteLine(time.ToShortDateString());

编辑:

如果它实际上是日以来,“1900”在这里是你如何能做到这一点:

public static DateTime DaysSince1900(int days)
{
    return new DateTime(1900, 1, 1).AddDays(days);
}



 DateTime time = DaysSince1900(39820);
 Console.WriteLine(time.ToShortDateString()); //will result in "1/9/2009"

这数目看起来像值“1900年以来的天数”。

当用Excel日期处理,日期可以是日期的字符串表示,或者它可以是一个OA日期。这是我写了一段时间的扩展方法返回到帮助促进日期转换:

/// <summary>
/// Sometimes the date from Excel is a string, other times it is an OA Date:
/// Excel stores date values as a Double representing the number of days from January 1, 1900.
/// Need to use the FromOADate method which takes a Double and converts to a Date.
/// OA = OLE Automation compatible.
/// </summary>
/// <param name="date">a string to parse into a date</param>
/// <returns>a DateTime value; if the string could not be parsed, returns DateTime.MinValue</returns>
public static DateTime ParseExcelDate( this string date )
{
    DateTime dt;
    if( DateTime.TryParse( date, out dt ) )
    {
        return dt;
    }

    double oaDate;
    if( double.TryParse( date, out oaDate ) )
    {
        return DateTime.FromOADate( oaDate );
    }

    return DateTime.MinValue;
}

只要在问题格式的单元格(一个或多个)作为日期,使用CTRL + 1,并选择您的所需的格式。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top