当偏移psented的UTC时间重新$ P $应用必须是年0到10,000之间。参数名:胶版 [英] The UTC time represented when the offset is applied must be between year 0 and 10,000. Parameter name: offset
问题描述
我在一个ASP.NET MVC3控制器以下code:
I have the following code in an ASP.NET MVC3 Controller:
public PartialViewResult GetCalendar(int? month, int? year)
{
var test = new DateTime((year.HasValue ? year.Value : 1), (month.HasValue ? month.Value : 1), 1);
return PartialView("Calendar", new DateTimeOffset(test));
}
我的看法型号是的DateTimeOffset?
什么是抛出的异常的原因是什么?
What is the reason for the exception thrown?
推荐答案
的的DateTimeOffset
构造函数首先将任何的DateTime
这是类
'UTC'到对应的UTC时间不。然后,它会检查是否UTC-相当于的DateTime
降至 DateTimeOffset.MinValue
的范围和<$ C $之外C> DateTimeOffset.MaxValue ,如果是的话,会抛出一个 ArgumentOutOfRangeException
类似于您所遇到的人。
The DateTimeOffset
constructor first converts any DateTime
that is not of Kind
'UTC' to the equivalent UTC time. It will then check whether the UTC-equivalent DateTime
falls outside of the bounds of DateTimeOffset.MinValue
and DateTimeOffset.MaxValue
, and if it does, will throw an ArgumentOutOfRangeException
similar to the one you are experiencing.
检查 DateTime.Kind
变量测试
您正在使用的,如果它不是UTC的,制定出如果转换为UTC将使的DateTime
按规定的测试
落在这些范围之外 - 根据MSDN文档中, MINVALUE
和 MaxValue的
(在UTC)是1/1/0001 12:00:00 AM +00:00'和'12 /9999分之31下午11:59:59 +00:00。分别为
Check the DateTime.Kind
of the variable test
that you are using, and if it is not 'UTC', work out if a conversion to UTC will make the DateTime
specified by test
fall outside of those bounds - according to the MSDN documentation, the MinValue
and MaxValue
(in UTC) are '1/1/0001 12:00:00 AM +00:00' and '12/31/9999 11:59:59 PM +00:00' respectively.
该文档(<一个href=\"http://msdn.microsoft.com/en-us/library/system.datetimeoffset.minvalue.aspx\">DateTimeOffset.MinValue)注意:
任何的DateTimeOffset值被转换为协调世界时(UTC)的方法执行与MINVALUE比较之前,这意味着一个的DateTimeOffset值,其日期和时间接近最低范围内,但其偏移量为正,可能会引发。一个例外例如,值1/1/0001上午01时00分○○秒+02:00超出范围,因为它比MINVALUE早1小时时,它被转换为UTC
"Any DateTimeOffset value is converted to Coordinated Universal Time (UTC) before the method performs the comparison with MinValue. This means that a DateTimeOffset value whose date and time are close to the minimum range, but whose offset is positive, may throw an exception. For example, the value 1/1/0001 1:00:00 AM +02:00 is out of range because it is one hour earlier than MinValue when it is converted to UTC."
和也(<一href=\"http://msdn.microsoft.com/en-us/library/system.datetimeoffset.maxvalue.aspx\">DateTimeOffset.MaxValue):
任何的DateTimeOffset值被转换为协调通用时间(UTC)之前的方法与MaxValue的进行比较。这意味着一个的DateTimeOffset值,其日期和时间接近最大范围内,但其偏移是负的,可能会引发。例外例如,值12/31/9999 11:00 PM -02:00超出范围,因为它是一个小时比的MaxValue后,当它被转换为UTC
"Any DateTimeOffset value is converted to Coordinated Universal Time (UTC) before the method compares it with MaxValue. This means that a DateTimeOffset value whose date and time are close to the maximum range, but whose offset is negative, may throw an exception. For example, the value 12/31/9999 11:00 PM -02:00 is out of range because it is one hour later than MaxValue when it is converted to UTC."
和按该文档(的DateTimeOffset构造的),则偏移被施加到一个非UTC 类
是本地系统的当前时区偏移。
And as per the docs (DateTimeOffset Constructor), the offset that is applied to a non-UTC Kind
is the "offset of the local system's current time zone".
这篇关于当偏移psented的UTC时间重新$ P $应用必须是年0到10,000之间。参数名:胶版的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!