Year, Month, and Day parameters describe an un-representable DateTime Exception - asp.net-mvc-3

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.

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.

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.

if the InsertDate meant to be the date / time of creation you can just use the following
DateTime InsertDate = DateTime.Now;

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.

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.

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
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.

The same exception I solved as:
DateTimePicker1.Value = DateSerial(Year(Now), Month(Now) + 1, 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

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);

Related

Carbon: comparing date instances is giving unexpected result

This problem is driving me crazy.
Here is my Carbon instance:
$carbonTemp = CarbonImmutable::createFromFormat('Y-m-d', $date);
What I want to do is just to understand if the date here is the last day of the week.
$carbonTemp->equalTo($carbonTemp->endOfWeek())
If $date = '2021-08-08' which is the last day of this week, the result above must be true, right?
It is giving me FALSE !!
Even though $carbonTemp->endOfWeek()->format('Y-m-d') is giving me '2021-08-08'.
endOfWeek() returns last day of week and time 23:59:59.999999. Therefore it is necessary to use endOfDay() method when creating date.
$carbonTemp = CarbonImmutable::createFromFormat('Y-m-d', $date)->endOfDay();
that's because you created CarbonImmutable object, so endOfWeek will not work.
Create a normal Carbon date:
$carbonTemp = Carbon::createFromFormat('Y-m-d', $date);

How pass month value and get start and end date of month In laravel

I am a new Laravel and using Laravel 5.5.
I want to pass value of month and get start and end date of passing month.
Suppose pass 5 (May month) the get value
Start date: 01-05-2019
End date: 30-05-2019
I have tried:
$finalMonth = 5;
$startMonth = Carbon::now()->addMonth($finalMonth)->day(1)->format("Y-m-d");
$endMonth = Carbon::now()->addMonth($finalMonth)->endOfMonth()->format("Y-m-d");
But It's given the wrong result because of it adds current month+pass month.
Means 4+5=9 that means sept.
Please Help
Instead of using:
$startMonth = Carbon::now()->addMonth($finalMonth)->day(1)->format("Y-m-d");
Use instead the month method:
$startMonth = Carbon::now()->month($finalMonth)->day(1)->format("Y-m-d");
See: carbon-get-month-by-number-returns-march-for-number-2
Use instead Below :
$startMonth = Carbon::now()->month($finalMonth)->day(1)->format("Y-m-d");

updating time of datetime field using linq

I have two fields in my database for storing starttime and endtime. they are of datetime. I pick time from them using tostring("hh:mm tt"). Now I want to update only the time part of the date. I have dropdownlist to select hour and minutes and AM/PM. How can I update the time of date stored in sql server using Entity framework / LINQ in MVC3 application.
Please suggest
actually you don't really need to touch the linq part of this, basically what you want to do is just to convert the string back to a datetime object and just manipulate the datetime object by either creating a new datetime object or add/minus mm/dd/yy hours or minutes.
You must always update the whole datetime - it means you must build a new DateTime in your application and use correct Date part and defined Time part.
create new DateTime Object
DateTime mydate = New DateTime(2011, 6, 1, 12, 30, 0);
or
DateTime mydate = DateTime.Parse("2011-06-1 12:30:00 PM");

Java calendar problem, JDK 1.6.0.22

I have a problem with getting the week of year. On my machine JDK 1.6.0.22 version is installed, on another machine 1.6.0.21. And both machines return different results:
(1.6.0.22) week is: 1 (1.6.0.21) week is: 52
For this code:
try {
Calendar current = new GregorianCalendar();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date d = df.parse("2010-12-28 19:04:38 GMT");
current.setTime(d);
int currentWeek = current.get(Calendar.WEEK_OF_YEAR);
System.out.println("week is: "currentWeek);
} catch (ParseException e) {
e.printStackTrace();
}
Why does JDK 1.6.0.22 give the wrong result?
This excerpt from the API documentation explains why this difference can occur:
Values calculated for the WEEK_OF_YEAR
field range from 1 to 53. Week 1 for a
year is the earliest seven day period
starting on getFirstDayOfWeek() that
contains at least
getMinimalDaysInFirstWeek() days from
that year. It thus depends on the
values of getMinimalDaysInFirstWeek(),
getFirstDayOfWeek(), and the day of
the week of January 1.
And from the source code of Calendar:
Both firstDayOfWeek and minimalDaysInFirstWeek are locale-dependent.
So it's the locale that determines this, not the time zone! Apparently, in some locales, week 1 of a year is considered to begin in the previous year. Try running this:
Calendar cal = new GregorianCalendar();
System.out.println(Locale.getDefault());
System.out.println(cal.getMinimalDaysInFirstWeek());
System.out.println(cal.getFirstDayOfWeek());
I bet that either you're running the different versions in different locales, or the locale data changed between these versions. The result "1" could even be the more correct one and due to a bug fix in the locale data.
Don't use the JDK date/time classes - use JODA Time instead.

Get the the most recent and the one before the most recent item

I have a table with many anniversaries : Date + Name.
I want to display the next anniversary and the one after with Linq.
How can i build the query ?
I use EF
Thanks
John
Just order by date and then use the .Take(n) functionality
Example with a list of some objects assuming you want to order by Date then Name:
List<Anniversaries> annivDates = GetAnnivDates();
List<Anniversaries> recentAnniv = annivDates.OrderBy(d => d.Date).ThenBy(d => d.Name).Take(2).ToList();
If the anniversaries are stored in regular DateTime structs, they may have the 'wrong' year set (i.e. wedding or birth year). I suggest writing a function which calculates the next date for an anniversary (based on the current day) like:
static DateTime CalcNext(DateTime anniversary) {
DateTime newDate = new DateTime(DateTime.Now.Year, anniversary.Month, anniversary.Day);
if (newDate < DateTime.Now.Date)
newDate = newDate.AddYear(1);
return newDate;
}
Then you proceed with sorting the dates and taking the first two values like described in the other postings:
(from e in anniversaries orderby CalcNext(e.Date) select e).Take(2)

Resources