Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Year, Month, and Day parameters describe an un-representable DateTime Exception

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.

like image 636
Sperick Avatar asked Jun 28 '12 14:06

Sperick


2 Answers

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.

like image 57
Jon Avatar answered Jan 01 '23 09:01

Jon


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.

like image 31
JabberwockyDecompiler Avatar answered Jan 01 '23 08:01

JabberwockyDecompiler