The UTC time represented when the offset is applied must be between year 0 and 10,000. Parameter name: offset

后端 未结 3 915
礼貌的吻别
礼貌的吻别 2020-12-17 09:06

I have the following code in an ASP.NET MVC3 Controller:

public PartialViewResult GetCalendar(int? month, int? year)
    {
        var test = new DateTime((y         


        
3条回答
  •  轮回少年
    2020-12-17 09:18

    I just had this issue, introduced by the part of my team that's on a negative UTC zone...

    What chamila_c posted is the real reason why this happens, but I needed a quick fix.

    To "solve it" I basically created this extension:

    public static class DateTimeExtensions
    {
        public static DateTimeOffset ToDateTimeOffset(this DateTime dateTime)
        {
            return dateTime.ToUniversalTime() <= DateTimeOffset.MinValue.UtcDateTime
                       ? DateTimeOffset.MinValue 
                       : new DateTimeOffset(dateTime);
        }
    }
    

    you might also want to check against the MaxValue.

提交回复
热议问题