C# Julian 日期解析器

发布于 2024-07-13 00:34:55 字数 195 浏览 8 评论 0原文

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

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

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#?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

仅此而已 2024-07-20 00:34:55

我认为 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

I think Excel is just using the standard OLE Automation DATE type which can be converted with the DateTime.FromOADate method.

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
泪是无色的血 2024-07-20 00:34:55

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"

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

            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:

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"
树深时见影 2024-07-20 00:34:55

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

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

め七分饶幸 2024-07-20 00:34:55

有两种方法可以执行此转换 --

  1. OLE 自动化日期方法 --

    double doubleDate = 43153.0; 
      日期时间正常日期 = DateTime.FromOADate(doubleDate); 
      /* {2/22/2018 12:00:00 上午} */ 
      
  2. DateAdd()方法,使用该方法,可以指定要转换成的日期类型

    double doubleDate = 43153.0; 
      DateTime NormalDate = new DateTime(1899, 12, 30, 0, 0, 0, DateTimeKind.Utc).AddDays(doubleDate); 
      /* {2/22/2018 12:00:00 上午} */ 
      

注意:这个日期常量 {12h December 31, 1899} 称为都柏林儒略日,记录在 儒略日

There are two ways to do this CONVERSION --

  1. OLE Automation Date method --

    double doubleDate = 43153.0;
    DateTime normalDate = DateTime.FromOADate(doubleDate);
    /* {2/22/2018 12:00:00 AM} */
    
  2. DateAdd() Method, using this method, you can specify the kind of date to convert to

    double doubleDate = 43153.0;
    DateTime normalDate = new DateTime(1899, 12, 30, 0, 0, 0, DateTimeKind.Utc).AddDays(doubleDate);
    /* {2/22/2018 12:00:00 AM} */
    

Note: This date constant {12h December 31, 1899} is called the Dublin Julian Day documented in Julian Day

微凉徒眸意 2024-07-20 00:34:55

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

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:

/// <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;
}
陌路黄昏 2024-07-20 00:34:55

只需将相关单元格格式设置为“日期”,使用 CTRL+1,然后选择所需的格式即可。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文