DateTime.AddDays 与 Calendar.AddDays
DateTime.AddDays
和 Calendar.AddDays
之间有什么区别?DateTime
类型日历独立吗?
What is the difference between DateTime.AddDays
and Calendar.AddDays
?
Is DateTime
type calendar independent?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
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/
我相信
DateTime
是硬编码的,可以有效地使用公历。例如,如果您查看
DateTime.DaysInMonth
假设有 12 个月,而
HebrewCalendar
支持 13。编辑:
DateTime
的某些方面确实容纳其他日历,例如 此构造函数。但是,我相信它只是将其转换为公历: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 theHebrewCalendar
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.AddDays
方法返回一个DateTime
对象并调用它的函数。As far as I know the
Calendar.AddDays
method returns aDateTime
object and calls it's function.这个问题的答案非常容易回答。两个功能没有区别。
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.