EXCEPTION: Year, Month, and Day parameters describe an un-representable DateTime Exception, StackTrace: at System.DateTime.DateToTicks(Int32 year, Int32 month, Int32 day)

beginer
beginer
1544 Points
52 Posts

I'm using DateTime to create new datetime instance as:

DateTime startDateUser = DateTime.UtcNow.Date.AddDays(100);
while (startDateUser.Date <= DateTime.UtcNow.Date)
{
    DateTime startDate = new DateTime(startDateUser.Year, startDateUser.Month, (startDateUser.Day - 1), 23, 59, 59);
    DateTime endDate = new DateTime(startDateUser.Year, startDateUser.Month, startDateUser.Day, 23, 59, 59);
    
    

    startDateUser = startDateUser.AddDays(1);

}

But getting error as:

EXCEPTION: Year, Month, and Day parameters describe an un-representable DateTime Exception, StackTrace: at System.DateTime.DateToTicks(Int32 year, Int32 month, Int32 day)
Views: 2777
Total Answered: 1
Total Marked As Answer: 1
Posted On: 13-Apr-2022 22:20

Share:   fb twitter linkedin
Answers
Smith
Smith
2890 Points
78 Posts
         

Subtract 1 day from DateTime object itself so that there will no chance for out range as follow:

DateTime startDateUser = DateTime.UtcNow.Date.AddDays(100);
while (startDateUser.Date <= DateTime.UtcNow.Date)
{
   var startDateUserDate = startDateUser.AddDays(-1);
    DateTime startDate = new DateTime(startDateUserDate.Year, startDateUserDate.Month, startDateUserDate.Day, 23, 59, 59);
    DateTime endDate = new DateTime(startDateUser.Year, startDateUser.Month, startDateUser.Day, 23, 59, 59);  

    startDateUser = startDateUser.AddDays(1);
}
Posted On: 19-Apr-2022 00:48
thanks. worked.
 - beginer  20-Apr-2022 23:52
 Log In to Chat