Last Sunday of the year in C #
I don't know if there is a method for this, but an easy way is to check the last 7 days of December starting from the 31st day and counting down.
Update: if the days of the week are numbered as follows:
0 Sun, 1 Mon, ... 6 Sat
then
lastSunday = 31 - DayOfWeek(31, 12, 2009) // pseudocode
source to share
You can find out what day of the week is December 31st. Then (if not Sunday) calculate back ...
for (int y=2009; y<=2018; y++)
{
DateTime dt = new DateTime(y, 12, 31); // last day of year
dt = dt.AddDays(-(int)dt.DayOfweek); // sun == 0, so then no change
Console.WriteLine(dt.ToString("dddd dd-MM-yyyy"));
}
source to share
It depends ... Do you want the last day of the year to be Sunday or Sunday of the last week of the year? Depending on how the weeks are defined in your country, the last week of a year can extend to six days the following year.
If you want to be the first, you can start on December 31st and go back until you find Sunday.
If you want the second, you will need to use the method System.Globalizartion.Calendar.GetWeekOfYear
to find out where the first week of the next year starts and take the day before.
source to share