我想使用c#将julian日期(YYJJJ格式)转换为任何常规日期格式(MMDDYY).是否有为此定义的功能? [英] I want to covert julian date(YYJJJ format) to any normal date format(MMDDYY) using c#. Is there any defined function for that?
问题描述
我有儒略历字符串 YYJJJ
格式.例如 05365
(2005年12月31日).我想转换为 MMDDYY
格式( 123105
).
Hi I have julian date string YYJJJ
format. eg 05365
(31st dec 2005). I want to covert to MMDDYY
format(123105
).
里面有定义的功能吗?
推荐答案
首先,没有 YY
, JJJ
和 DD
格式为自定义日期和时间格式.一种解决方案可能是将字符串 Year
和 JulianCalendar
类./p>
First of all, there is no YY
, JJJ
and DD
formats as a custom date and time format. One solution might be to split your string Year
and DayOfYear
part and create a DateTime
with JulianCalendar
class.
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(..
"部分,因为
I initialized this new DateTime(..
part with 18th December because
来自朱利安日历
因此,儒略历现在比日历晚了 13 天.公历;例如,儒略历中的1月1日是1月14日在公历.
Consequently, the Julian calendar is currently 13 days behind the Gregorian calendar; for instance, 1 January in the Julian calendar is 14 January in the Gregorian.
您可以像这样设置 dt
格式;
And you can format your dt
like;
dt.ToString("MMddyy", CultureInfo.InvariantCulture) //123105
老实说,我不喜欢这种方式,但这是我能想到的唯一解决方案.
I honestly didn't like this way but this is the only one I can imagine as a solution.
这篇关于我想使用c#将julian日期(YYJJJ格式)转换为任何常规日期格式(MMDDYY).是否有为此定义的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!