我无法在C#中将波斯语(太阳Hijri日历)日期表示为 DateTime
,特别是在特定月份的某些日子,例如31/04,在格里高利历中这样的日期没有意义:
System.Globalization.PersianCalendar p = new System.Globalization.PersianCalendar();
DateTime date = new DateTime(2013,7,22);
int year = p.GetYear(date);
int month = p.GetMonth(date);
int day = p.GetDayOfMonth(date);
DateTime d1 = new DateTime(year, month, day);
上面的代码将导致 ArgumentOutOfRangeException
说: Year, Month, and Day parameters describe an un-representable DateTime.
这是预期的 .
我如何在.NET中将波斯日期表示为 DateTime
s考虑日期,例如30/02和31/04?
2 回答
是的,因为除非您指定日历,否则
DateTime
参数应为格里高利 . 您可以指定日历:请注意,如果你现在拿
d1.Year
,你会回到2013年,而不是year
...DateTime
总是格里高利,基本上 . 但是,如果您使用具有波斯日历作为默认日历的文化,则在将DateTime
格式化为字符串时,将适当地转换值 . 编辑:不幸的是,根据documentation:正如评论中提到Noda Time,我可以解决这个问题:它还不支持波斯日历 . 它支持月球Hijri日历,但不支持太阳能日历:(我可以考虑将其添加到未来版本中...
来自MSDN;
也来自PersianCalendar Class
你想要什么对我来说似乎不可能 .