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 |
int daysOffset = date.DayOfWeek - dayOfWeek; // take the offset to subtract directly instead of looping |
||
if ( |
if (daysOffset < 0) daysOffset += 7; // if the code is negative, we need to normalize them |
||
date = date.AddDays(- |
date = date.AddDays(-daysOffset ); // now just add the days offset |
||
Console.WriteLine(date.ToString("yyyy-MM-dd")); |
Console.WriteLine(date.ToString("yyyy-MM-dd")); |
||
} |
} |