我想使用c#将朱利安日期(YYJJJ格式)转换为任何正常日期格式(MMDDYY)。那是否有任何定义的功能?

时间:2014-10-28 06:44:07

标签: c# datetime

您好我有朱利安日期字符串YYJJJ格式。例如05365(2005年12月31日)。我想转换为MMDDYY格式(123105)。

是否有任何已定义的功能?

2 个答案:

答案 0 :(得分:2)

我遇到了同样的问题,因为我试图将日期从BACS 18标准转换为字符串。我无法找到解决这个问题的方法,所以我写了这个函数:

    private String bacsDateConvert(String bacsFormatDate)
    {
        int dateYear = Convert.ToInt16(bacsFormatDate.Substring(1, 2));
        int dateDays = Convert.ToInt16(bacsFormatDate.Substring(3, 3));

        DateTime outputDate = new DateTime();
        outputDate = Convert.ToDateTime("31-12-1999");

        outputDate = outputDate.AddYears(dateYear);
        outputDate = outputDate.AddDays(dateDays);

        String outputString = outputDate.ToString("yyyyMMdd");


        return outputString;
    }

//您可以这样称呼它: textBox4.Text = Convert.ToString(bacsDateConvert(bacsTxnValueDate));

如果您愿意,也可以稍微修改它并轻松返回DateTime数据类型。我只需要以上述格式返回一个字符串。

答案 1 :(得分:0)

首先,没有YYJJJDD格式为custom date and time format。一个解决方案可能分割您的字符串YearDayOfYear部分,并创建一个DateTimeJulianCalendar

string s = "05365";
int year = Convert.ToInt32(s.Substring(0, 2));
// Get year part from your string
int dayofyear = Convert.ToInt32(s.Substring(2));
// Get day of years part from your string
DateTime dt = new DateTime(1999 + year, 12, 18, new JulianCalendar());
// Initialize a new DateTime one day before year value. 
// Added 1999 to year part because it makes 5 AD as a year if we don't.
// In our case, it is 2004/12/31
dt = dt.AddDays(dayofyear);
// Since we have a last day of one year before, we can add dayofyear to get exact date

我在12月18日初始化了这个new DateTime(..部分,因为

来自Julian Calendar

  

因此,朱利安日历目前 13 天后   阳历日历;例如,1月1日的朱利安历法是   1月14日在格里高利。

您可以格式化dt喜欢;

dt.ToString("MMddyy", CultureInfo.InvariantCulture) //123105

老实说,我不喜欢这种方式,但这是我唯一可以想象的解决方案。