C# Julian 日期解析器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/513247/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 06:20:47  来源:igfitidea点击:

C# Julian Date Parser

c#.netjulian-date

提问by Jake Pearson

I have a cell in a spreadsheet that is a date object in Excel but becomes a double (something like 39820.0 for 1/7/2009) when it comes out of C1's xls class. I read this is a Julian date format. Can someone tell me how to parse it back into a DateTime in C#?

我在电子表格中有一个单元格,它是 Excel 中的日期对象,但是当它从 C1 的 xls 类中出来时变成了双精度(类似于 2009 年 1 月 7 日的 39820.0)。我读到这是儒略日期格式。有人能告诉我如何将它解析回 C# 中的 DateTime 吗?

Update: It looks like I might not have a Julian date, but instead the number of days since Dec 30, 1899.

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

采纳答案by GBegen

I think Excel is just using the standard OLE Automation DATEtype which can be converted with the DateTime.FromOADatemethod.

我认为 Excel 只是使用标准的 OLE 自动化DATE类型,可以使用该DateTime.FromOADate方法进行转换。

This block of code,

这段代码,

using System;

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

outputs:

输出:

1/7/2009 12:00:00 AM

回答by Kieron

That number looks like a 'number of days since 1900' value.

该数字看起来像是“自 1900 年以来的天数”值。

回答by BFree

There's a JulianCalendar class in System.Globalization; Here's how you would use it:

System.Globalization 中有一个 JulianCalendar 类;以下是您将如何使用它:

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

EDIT:

编辑:

If it is in fact days since "1900" here's how you can do it:

如果实际上是自“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"

回答by Metro Smurf

When dealing with Excel dates, the date may be the string representation of a date, or it may be an OA date. This is an extension method I wrote a while back to help facilitate the date conversion:

在处理 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;
}

回答by Madcat

Just format the cell(s) in question as Date, use CTRL+1, and select the your desired format.

只需将有问题的单元格格式化为日期,使用 CTRL+1,然后选择所需的格式。