回历和公历 DateTime 构造函数

Hijri and Gregorian DateTime constructor (回历和公历 DateTime 构造函数)

本文介绍了回历和公历 DateTime 构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

传递给 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 构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:回历和公历 DateTime 构造函数

基础教程推荐