DateTime.AddDays 与 Calendar.AddDays

发布于 2024-10-14 04:12:16 字数 112 浏览 0 评论 0原文

DateTime.AddDaysCalendar.AddDays 之间有什么区别?
DateTime 类型日历独立吗?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

秋日私语 2024-10-21 04:12:16

DateTime.AddDays 只是将天数转换为刻度,并将刻度数添加到日期时间中。 Calendar.AddDays 的默认实现的作用完全相同。但是,由于它是一种虚拟方法,因此可以以更复杂的方式在特定日历中实现,例如: http://codeblog.jonskeet.uk/2010/12/01/the-joys-of-date-time-arithmetic/

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/

喜爱皱眉﹌ 2024-10-21 04:12:16

我相信 DateTime 是硬编码的,可以有效地使用公历。

例如,如果您查看 DateTime.DaysInMonth假设有 12 个月,而 HebrewCalendar 支持 13。

编辑:DateTime 的某些方面确实容纳其他日历,例如 此构造函数。但是,我相信它只是将其转换为公历:

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

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
愛放△進行李 2024-10-21 04:12:16

据我所知,Calendar.AddDays方法返回一个DateTime对象并调用它的函数。

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

余生一个溪 2024-10-21 04:12:16

这个问题的答案非常容易回答。两个功能没有区别。

Calendar.AddDays 也是一个 DateTime

DateTime不扩展该特定功能,它唯一使用的是 UTC 和本地时间。人们还应该建议日历不是 DateTime 对象,它的行为可能不同,并且似乎没有提供获取当前系统时间的方法。

编辑 - 我最初认为您谈论的是 Web 控制,这似乎是一种全球化,允许您显示给定用户群的当前日期和时间,以执行其声明的操作系统设置。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文