应用偏移量时所表示的UTC时间必须介于0到10,000之间。参数名称:offset [英] The UTC time represented when the offset is applied must be between year 0 and 10,000. Parameter name: offset
问题描述
public PartialViewResult GetCalendar(int?month,int?年)
{
var test = new DateTime((year.HasValue?year.Value:1),(month.HasValue?month.Value:1),1);
return PartialView(Calendar,new DateTimeOffset(test));
}
我的视图模型是 DateTimeOffset?
引发异常的原因是什么?
p> DateTimeOffset
构造函数首先将不是类$ c>的任何
DateTime
$ c>'UTC'等效于UTC时间。然后,它将检查UTC相当的 DateTime
是否超出了 DateTimeOffset.MinValue
和 DateTimeOffset.MaxValue
,如果有的话,将会抛出一个与您遇到的类似的 ArgumentOutOfRangeException
。
检查您正在使用的变量 test
的 DateTime.Kind
,如果它不是UTC,如果转换为UTC将使得 test
指定的 DateTime
不在这些边界 - 根据MSDN文档, MinValue
和 MaxValue
(以UTC为单位)为1/1/0001 12:00:00 AM +00:00'和'12 / 31/9999 11:59:59 PM +00:00'。
文档(< a href =http://msdn.microsoft.com/en-us/library/system.datetimeoffset.minvalue.aspx =noreferrer> DateTimeOffset.MinValue )请注意:
任何DateTimeOffset值都将转换为Coordinated Universal方法执行与MinValue进行比较之前的时间(UTC)。这意味着日期和时间接近最小范围但偏移量为正的DateTimeOffset值可能会引发异常。例如,值为1/1/0001 1:00:00 AM +02:00是超出范围的,因为它比MinValue提前了一个小时,当它被转换为UTC时。
还有( DateTimeOffset.MaxValue ):
在方法将其与MaxValue进行比较之前,任何DateTimeOffset值都将转换为协调世界时(UTC)。这意味着日期和时间接近最大范围但偏移为负的DateTimeOffset值可能会引发异常。例如,值12/31/9999 11:00 PM -02:00超出范围,因为它在转换为UTC时比MaxValue晚一个小时。
根据文档( DateTimeOffset构造函数),偏移量应用于非UTC 种类
是本地系统当前时区的偏移量。
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));
}
My view model is DateTimeOffset?
What is the reason for the exception thrown?
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.
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.
The docs (DateTimeOffset.MinValue) note that:
"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."
And also (DateTimeOffset.MaxValue):
"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."
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".
这篇关于应用偏移量时所表示的UTC时间必须介于0到10,000之间。参数名称:offset的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!