What is the difference between DateTime.AddDays and Calendar.AddDays?
Is DateTime type calendar independent?
4 Answers
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/
- 94,086
- 102
- 195
- 339
- 5,074
- 22
- 31
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
- 1,335,956
- 823
- 8,931
- 9,049
-
Thanks. Reflector shows that they use calendar.ToDateTime(year, month, day, hour, minute, second, 0).Ticks inside constructor. – Jarlaxle Jan 25 '11 at 13:19
As far as I know the Calendar.AddDays method returns a DateTime object and calls it's function.
- 17,025
- 1
- 40
- 63
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.
- 2,511
- 3
- 24
- 41