Year, Month, and Day parameters describe an un-rep

2019-04-28 06:06发布

问题:

I'm adding an object to a list within an ASP.NET MVC 3 application using the following code but one of the properties of the object is giving me difficulties.

ls.Add(new UserRoleModel { UserRoleId = 1, UserName = "Paul", InsertDate = new DateTime(05, 24, 2012),InsertProgram="sqlplus",InsertUser="sp22",Role="Implementation Co-corindator"});

This builds but when I go to the relevant page i get an Exception with the following type:

Exception Details: System.ArgumentOutOfRangeException: Year, Month, and Day parameters describe an un-representable DateTime.

I've tried removing the 0 in the month but I get the same exception.

回答1:

The DateTime constructor you are calling accepts parameters in the order year, month, day.

You are providing them in the order month, day, year -- which ends up trying to assign meaningless values. The documentation is pretty clear on what the allowed values are and what happens if you pass 2012 for the "day" value.



回答2:

I just ran into this and my issue was I was creating a date in February. I tried to do the following...

new Date(2013, 2, 30)

Since there is not a February 30th, the date failed to create. When I changed to

new Date(2013, 2, 28)

it worked fine.



回答3:

if the InsertDate meant to be the date / time of creation you can just use the following

DateTime InsertDate  = DateTime.Now;


回答4:

You are using an Invalid datetime, like for month you may be passing 13,14 days more than 31

or something like that which is causing the issue.



回答5:

i was having the same issue because i was trying to create a DateTime object with date set to 31 of April even though April only has 30 days. Anyone having the same issue, make sure you are not making this mistake.