我试图将GregorianCalendar转换成波斯日历
这是我的方法:
public static DateTime GetFdate(string _Edate)
{
DateTime fdate = Convert.ToDateTime(_Edate);
GregorianCalendar gcalendar = new GregorianCalendar();
PersianCalendar pcalendar = new PersianCalendar();
DateTime fDate = gcalendar.ToDateTime(
pcalendar.GetYear(fdate),
pcalendar.GetMonth(fdate),
pcalendar.GetDayOfMonth(fdate),
pcalendar.GetHour(fdate),
pcalendar.GetMinute(fdate),
pcalendar.GetSecond(fdate), 0);
return fDate;
}
Run Code Online (Sandbox Code Playgroud)
问题是,它不适用于所有日期:
DateTime dt = GetFdate("2015-07-22 00:00:00.000");
Run Code Online (Sandbox Code Playgroud)
它给出了这个错误:
An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll
Additional information: Year, Month, and Day parameters describe an un-representable DateTime.
Run Code Online (Sandbox Code Playgroud)
但对于其他日期,它的工作原理如下:
DateTime dt = GetFdate("2015-06-29 00:00:00.000");
Run Code Online (Sandbox Code Playgroud)
引发参数异常是因为您尝试创建在公历中无效的日期。
"2015-07-22 00:00:00.000"当您检查从波斯历的公历日期获得的值时
pcalendar.GetYear(fdate).Dump();
pcalendar.GetMonth(fdate).Dump();
pcalendar.GetDayOfMonth(fdate).Dump();
Run Code Online (Sandbox Code Playgroud)
您将得到 1394 4 31,这对于波斯日历有效,正如 MSDN 上的注释所解释的那样:
波斯历的前六个月每月有 31 天,接下来的五个月每月有 30 天,最后一个月平年有 29 天,闰年有 30 天。
显然,当您将其输入公历时,您会遇到麻烦,因为 4 月没有 31 天:
公历有 12 个月,每个月有 28 至 31 天:一月(31 天)、二月(28 或 29 天)、三月(31 天)、四月(30 天)、五月(31 天)、六月(30 天) 、7 月(31 天)、8 月(31 天)、9 月(30 天)、10 月(31 天)、11 月(30 天)和 12 月(31 天)。
公历日期"2015-06-29 00:00:00.000"不会引发异常,因为波斯历的年、月、日结果为 1394 4 8,这对于公历也是可接受的。
DateTime 被视为公历,要转换为日历中的特定年、月或日,请调用您感兴趣的日历的GetYear,GetMonth或GetDayOfMonth,例如如下所示
| 归档时间: |
|
| 查看次数: |
524 次 |
| 最近记录: |