您好我有朱利安日期字符串YYJJJ
格式。例如05365
(2005年12月31日)。我想转换为MMDDYY
格式(123105
)。
是否有任何已定义的功能?
答案 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)
首先,没有YY
,JJJ
和DD
格式为custom date and time format。一个解决方案可能分割您的字符串Year
和DayOfYear
部分,并创建一个DateTime
类JulianCalendar
。
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(..
部分,因为
因此,朱利安日历目前 13 天后 阳历日历;例如,1月1日的朱利安历法是 1月14日在格里高利。
您可以格式化dt
喜欢;
dt.ToString("MMddyy", CultureInfo.InvariantCulture) //123105
老实说,我不喜欢这种方式,但这是我唯一可以想象的解决方案。