Reputation: 901
What is the difference between DateTime.AddDays
and Calendar.AddDays
?
Is DateTime
type calendar independent?
Upvotes: 4
Views: 2211
Reputation: 5327
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/
Upvotes: 10
Reputation: 2551
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.
Upvotes: 0
Reputation: 1500675
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
Upvotes: 7
Reputation: 17631
As far as I know the Calendar.AddDays
method returns a DateTime
object and calls it's function.
Upvotes: 0