nwadc10

I am having trouble with DateTime throwing the out of range exception. The code works fine on my system but has difficulty on mostly non-USA users systems.

The offending code is:

Code Snippet

System::DateTime dtRef = System::DateTime(nYear, 01, 01);

System::DateTime dtDate = dtRef.AddDays(nDayofYear - 1);

szDate = dtDate.ToString("yyyy/MM/dd");

The only thing I can come up with is that the culture/region settings of non-USA users present the date as dd/mm/yyyy normally. When I call the AddDays method days are in effect being added to the month instead of day causing the month to be out of range throwing the exception. Any ideas of what is happening in the code



Re: Visual C++ Express Edition DateTime throws ArgumentOutOfRangeException

nobugz

No, that can't be it. The code can certainly generate ArgumentOutOfRange, nYear < 1 or nYear > 9999. Or nDayOfYear drastically out of range.





Re: Visual C++ Express Edition DateTime throws ArgumentOutOfRangeException

nwadc10

I'm happy to hear it's not a culture/region issue because I didn't know how I was going to fix that When I debug thru the program the nYear is within limits (2007) and nDayofYear is a julian date of 231, for example. The exception is being thrown by the first line of code above where I initialize the date.





Re: Visual C++ Express Edition DateTime throws ArgumentOutOfRangeException

nobugz

Why don't you add a separate check on the nYear value:

if (nYear < 1 || nYear > 9999) throw gcnew Exception("Year out of range");