我在電子表格中有一個單元格,它是Excel中的日期對象,但當它出現在C1的xls類中時,它變成了一個雙精度元素(類似於2009年1月7日的39820.0)。我讀到這是Julian日期格式。有人可以告訴我如何解析回到C#中的DateTime嗎?C#Julian Date Parser
更新:它看起來像我可能沒有一個Julian日期,而是因爲12月30日的天數,1899年
我在電子表格中有一個單元格,它是Excel中的日期對象,但當它出現在C1的xls類中時,它變成了一個雙精度元素(類似於2009年1月7日的39820.0)。我讀到這是Julian日期格式。有人可以告訴我如何解析回到C#中的DateTime嗎?C#Julian Date Parser
更新:它看起來像我可能沒有一個Julian日期,而是因爲12月30日的天數,1899年
我認爲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
這個數字看起來像一個「號自1900年以來的天數「。
我認爲你可能是對的,解析這個的正確方法是什麼? – 2009-02-04 20:54:04
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"
看起來像擴展方法的一個很好的候選人:DateTime.Since1900(...) – 2009-02-04 21:00:19
當用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,然後選擇你想要的格式。
我試過你鏈接到的功能,但是,我的號碼沒有解析。也許我沒有朱利安。 – 2009-02-04 20:54:46