問題描述
傳遞給 DateTime 類型構造函數的 Calendar 對象的正確行為是什么?
what is the correct behavior for the Calendar objected passed to the constructor of DateTime type?
我有如下示例的年、月和日組件:
I have the components year, month and day as the below example:
day = 1
month = 5
year = 1433 (which is the current Hijri year)
當使用下面的代碼創建一個日期時間對象時,結果是一個有效的格雷格日期
when creating a datetime object using the below code the result is a valid Greg Date
HijriCalendar hijri = new HijriCalendar();
//Get the First Day in the Month
DateTime firstDayInMonth = new DateTime(1433, month, 1, hijri);
使用下面的代碼會生成一個有效的回歷日期:
while using the below code generates a valid Hijri date:
GregorianCalendar greg = new GregorianCalendar();
//Get the First Day in the Month
DateTime firstDayInMonth = new DateTime(1433, month, 1, greg);
這是正確的結果嗎?
推薦答案
你的第一個例子是正確的.DateTime 不會采用 Hijri 格式,它只是您給它的標準化等價物.如何獲取 Hirji 日期請參見以下代碼:
Your first example is correct. The DateTime will not be in the Hijri format, it will just be the standardised equivalent of what you gave it. See the following code for how to get the Hirji date:
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",因此當您檢查它時,您并沒有得到 hirji 日期,而只是得到了您在 15 世紀給出的日期.
Your second block of code was just setting the gregorian date "1/1/1433" so when you were inspecting it you weren't getting a hirji date, you were just getting the date you gave it in the 15th century.
查看 http://msdn.microsoft.com/en-us/library/system.globalization.hijricalendar.aspx 并查看其中的方法應該可以讓您更好地了解應該在日歷對象上做什么以及在 DateTime 對象上應該發生什么.
Looking at http://msdn.microsoft.com/en-us/library/system.globalization.hijricalendar.aspx and seeing the methods there should give you a better idea of what you should be doing on the calendar object and what should happen on the DateTime object.
這篇關于回歷和公歷 DateTime 構造函數的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!