Sperick
Sperick

Reputation: 2761

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.

Upvotes: 24

Views: 111084

Answers (10)

Omar Laracuente
Omar Laracuente

Reputation: 81

I'm encountering an ArgumentOutOfRangeException when trying to read a date value from an Oracle database. The value in the table is '30-MAR-63'. Here's the code snippet that's causing the issue:

try
{
    var value = reader["some_date"]; // '30-MAR-63' is the value in the table
}
catch (ArgumentOutOfRangeException ex)
{
    // The exception is thrown here
    // System.ArgumentOutOfRangeException: 'Year, Month, and Day parameters describe an un-representable DateTime.'
}

Upvotes: 0

The Oathman
The Oathman

Reputation: 165

if you got this error because you're trying to insert 29 days in a year that's not a leap year, then you can check the maximum allowed days first of that year as such:

if(DateTime.DaysInMonth(2024,2) == 29)
   dt = new DateTime(2024, 2, 29);
   

Upvotes: 1

Alexandra
Alexandra

Reputation: 1

For those who has this issue, make sure that date, which you try to create exists. For example, I had this problem, because was trying to create 29 feb at not leap year

Upvotes: 0

Srijon Chakraborty
Srijon Chakraborty

Reputation: 2164

Simply you have to change the InsertDate into

InsertDate = new DateTime(Year,Month,Date);

It will solve your problem.

I was facing the same type of issue, where I was using a DateTimePicker in Winform Application. Where user can only pick only month and year and my code and UI was look like enter image description here

Code

            this.dtpMonth.CustomFormat = "MMMM yyyy";
            this.dtpMonth.Font = new System.Drawing.Font("Verdana", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.dtpMonth.Format = System.Windows.Forms.DateTimePickerFormat.Custom;
            this.dtpMonth.Location = new System.Drawing.Point(88, 24);
            this.dtpMonth.Name = "dtpMonth";
            this.dtpMonth.ShowUpDown = true;
            this.dtpMonth.Size = new System.Drawing.Size(136, 22);
            this.dtpMonth.TabIndex = 1;

And I was initializing the by this.dtpMonth=DateTime.Today; and this line cost me and error of current discussion.
Afterward, I have found that my date(DateTime.Today) was 31 July. So, when user want change the month to August it throws exception because August doesn't have date 31. Finally, I change my code into

 dtpMonth.Value =new DateTime(DateTime.Today.Year, DateTime.Today.Month,1);

And worked for me.

Upvotes: 1

Khan
Khan

Reputation: 1

The same exception I solved as:

DateTimePicker1.Value = DateSerial(Year(Now), Month(Now) + 1, 1)

Upvotes: 0

Fakhar Ahmad Rasul
Fakhar Ahmad Rasul

Reputation: 1691

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.

Upvotes: 1

Ajay Suwalka
Ajay Suwalka

Reputation: 531

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.

Upvotes: 1

JabberwockyDecompiler
JabberwockyDecompiler

Reputation: 3390

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.

Upvotes: 12

spooti
spooti

Reputation: 247

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

DateTime InsertDate  = DateTime.Now;

Upvotes: 2

Jon
Jon

Reputation: 437784

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.

Upvotes: 42

Related Questions