开发者

DateTime.AddDays vs Calendar.AddDays

开发者 https://www.devze.com 2023-02-06 09:46 出处:网络
What is the difference between DateTime.AddDays开发者_如何学运维 and Calendar.AddDays? Is DateTime type calendar independent?DateTime.AddDays just converts days to ticks and adds this number of ticks

What is the difference between DateTime.AddDays开发者_如何学运维 and Calendar.AddDays?

Is DateTime type calendar independent?


DateTime.AddDays just converts days to ticks and adds this number of ticks to the date time. The default implementation of Calendar.AddDays does exactly the same. However, since it is a virtual method it can be implemented in specific calendar in a more complicated way, e.g. something like here: http://codeblog.jonskeet.uk/2010/12/01/the-joys-of-date-time-arithmetic/


I believe that DateTime is hard-coded to use the Gregorian calendar, effectively.

For example, if you look at DateTime.DaysInMonth it assumes there are 12 months, whereas the HebrewCalendar supports 13.

EDIT: There are some aspects of DateTime which do accommodate other calendars, such as this constructor. However, I believe it just converts it to a Gregorian calendar:

Calendar calendar = new HebrewCalendar();
DateTime dt = new DateTime(5901, 13, 1, 0, 0, 0, calendar); // Uses month 13!
Console.WriteLine(dt.Year); // 2141
Console.WriteLine(dt.Month); // 9


As far as I know the Calendar.AddDays method returns a DateTime object and calls it's function.


The answer to this question is extremely easy to answer. There is no difference between the two functions.

Calendar.AddDays is also a DateTime

DateTime does not extend that particular functionality the only thing it uses is UTC and Local time. One should also suggest that a Calendar is NOT a DateTime object, it might not behave the same, and does not appear to provide a method to get the current system time.

Edit - I originally thought you were talking about the Web Control, this appears to be a globalization, to allow you to display the current date and time for a given user base do their declared operating system's settings.

0

精彩评论

暂无评论...
验证码 换一张
取 消