Hijri和Gregorian DateTime构造函数
对象传递给DateTime类型的构造函数的正确行为是什么?
我有组件年,月和日如下例:
day = 1 month = 5 year = 1433 (which is the current Hijri year)
使用以下代码创建日期时间对象时,结果是有效的Greg日期
HijriCalendar hijri = new HijriCalendar(); //Get the First Day in the Month DateTime firstDayInMonth = new DateTime(1433, month, 1, hijri);
使用以下代码生成有效的Hijri日期:
GregorianCalendar greg = new GregorianCalendar(); //Get the First Day in the Month DateTime firstDayInMonth = new DateTime(1433, month, 1, greg);
这是正确的结果吗?
你的第一个例子是正确的。 DateTime不是Hijri格式,它只是你给它的标准化等价物。 有关如何获取Hirji日期,请参阅以下代码:
HijriCalendar hijri = new HijriCalendar(); DateTime firstDayInMonth = new DateTime(1433, 10, 11, hijri); Console.WriteLine(hijri.GetEra(firstDayInMonth)); // 1 Console.WriteLine(hijri.GetYear(firstDayInMonth)); // 1433 Console.WriteLine(hijri.GetMonth(firstDayInMonth)); // 10 Console.WriteLine(hijri.GetDayOfMonth(firstDayInMonth)); // 11
你的第二段代码只是设置了格里高利日期“1/1/1433”所以当你检查它时你没有得到一个日期,你只是得到了你在15世纪给它的日期。
查看http://msdn.microsoft.com/en-us/library/system.globalization.hijricalendar.aspx并查看其中的方法应该可以让您更好地了解应该在日历对象上执行的操作以及应该执行的操作在DateTime对象上。
你实际上并没有问过一个有意义的问题。 如果您试图将给定日期从一个日历转换为另一个日历,那么在所有Hijri日历与格里高利不同的月份之后,日期将会更改。
查看此站点的示例 – 它甚至可以下载代码。