Find the last Sunday of each month: Difference between revisions

Content added Content deleted
Line 804: Line 804:
*/
*/
// The updated code
// The updated code
int daysOffest = date.DayOfWeek - dayOfWeek; // take the offset to subtract directly instead of looping
int daysOffset = date.DayOfWeek - dayOfWeek; // take the offset to subtract directly instead of looping
if (daysOffest < 0) daysOffest += 7; // if the code is negative, we need to normalize them
if (daysOffset < 0) daysOffset += 7; // if the code is negative, we need to normalize them
date = date.AddDays(-daysOffest); // now just add the days offset
date = date.AddDays(-daysOffset ); // now just add the days offset
Console.WriteLine(date.ToString("yyyy-MM-dd"));
Console.WriteLine(date.ToString("yyyy-MM-dd"));
}
}