4

What is the difference between DateTime.AddDays and Calendar.AddDays?
Is DateTime type calendar independent?

shA.t
  • 16,580
  • 5
  • 54
  • 111
Jarlaxle
  • 871
  • 10
  • 18

4 Answers4

10

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/

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Konstantin Oznobihin
  • 5,234
  • 24
  • 31
7

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
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 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
0

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

Matten
  • 17,365
  • 2
  • 42
  • 64
0

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.

Security Hound
  • 2,577
  • 3
  • 25
  • 42