What is the difference between DateTime.AddDays
and Calendar.AddDays
?
Is DateTime
type calendar independent?
问题:
回答1:
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/
回答2:
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
回答3:
As far as I know the Calendar.AddDays
method returns a DateTime
object and calls it's function.
回答4:
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.