Hello all, I've written an extension method that, from a datetime input parameter, gets me the last sunday of the month
public static DateTime LastWeekDayOfTheMonth(this DateTime date, DayOfWeek dayOfWeek) { var day = new DateTime(date.Year, date.Month + 1, 1).AddDays(-1);
while (day.DayOfWeek != dayOfWeek) { day = day.AddDays(-1); }
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
Gerry, the code in the original post uses a parameter to determine which weekday is required. So the original code will return the last Tuesday or Friday as required. Your longer, more complex code seems to be hardcoded to Sunday. You could simplify your
code by creating an extension method as the original post asked for.
"from a datetime input parameter, gets me the
last sunday of the month"
My solution is simple, short and not complex ... given the last day of the month, using existing .NET Framework capabilities, finding the last Sunday requires only two lines and
no while loop:
meanwhile, computing the last day of the month is equally simple; given a year and a month, it simply uses the .DaysInMonth method to construct the end of the month date thanks to this constructor:
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
ciupazMI
Member
140 Points
172 Posts
Get the last Sunday of a month
Dec 18, 2012 07:11 PM|LINK
Hello all, I've written an extension method that, from a datetime input parameter, gets me the last sunday of the month
public static DateTime LastWeekDayOfTheMonth(this DateTime date, DayOfWeek dayOfWeek) { var day = new DateTime(date.Year, date.Month + 1, 1).AddDays(-1);
while (day.DayOfWeek != dayOfWeek) { day = day.AddDays(-1); }
return day; }
and then I'll call it like:
DateTime LastSundayOfTheMonth = Convert.ToDateTime(MyDate).LastWeekDayOfTheMonth(DayOfWeek.Sunday);
This extension method has a bug if the month is December.
How can I solve this problem?
Luigi
Paul Linton
Star
13581 Points
2571 Posts
Re: Get the last Sunday of a month
Dec 18, 2012 07:58 PM|LINK
Change the first line to
var day = new DateTime(date.Year, date.Month, 1).AddMonths(1).AddDays(-1);
gerrylowry
All-Star
20577 Points
5721 Posts
Re: Get the last Sunday of a month
Dec 19, 2012 04:26 AM|LINK
@ ciupazMI
The .NET Framework makes this easy:
here is a simple test method to show the output:
public static void cwTestResult(DateTime lastDayOfMonth, DateTime lastSunday) { Console.WriteLine("lastDayOfMonth {0}, lastSunday {1}", lastDayOfMonth.ToString("yyyy-MMMMM-dd dddd"), lastSunday.ToString("yyyy-MMMMM-dd dddd")); }first we need to get the last day of the month:
public static Boolean LastDayOfMonth(Int32 year, Int32 month, out DateTime lastDayOfMonth) { Int32 lastDayOfYearMonth; try{ lastDayOfYearMonth = DateTime.DaysInMonth(year, month); } catch (ArgumentOutOfRangeException badData) { Console.WriteLine (badData.Message); // .NET error message lastDayOfMonth = new DateTime(9999,12,31,23,59,59); // we failed 9999-12-31 23:59:59 return false; } lastDayOfMonth = new DateTime(year, month, lastDayOfYearMonth); return true; }from the last day of the month, getting the last Sunday is easy:
public static DateTime LastSunday(DateTime lastDayOfMonth) { Int32 dayValue = (Int32)lastDayOfMonth.DayOfWeek; return lastDayOfMonth.AddDays(-1*dayValue); }Here is some code to test this:
void Main() { DateTime lastDayOfMonth; DateTime lastSunday; if(LastDayOfMonth(2012, 1, out lastDayOfMonth)) // Tuesday { lastSunday = LastSunday(lastDayOfMonth); cwTestResult(lastDayOfMonth, lastSunday); } if(LastDayOfMonth(2012, 2, out lastDayOfMonth)) // Wednesday { lastSunday = LastSunday(lastDayOfMonth); cwTestResult(lastDayOfMonth, lastSunday); } if(LastDayOfMonth(2012, 3, out lastDayOfMonth)) // Saturday { lastSunday = LastSunday(lastDayOfMonth); cwTestResult(lastDayOfMonth, lastSunday); } if(LastDayOfMonth(2012, 12, out lastDayOfMonth)) // Monday { lastSunday = LastSunday(lastDayOfMonth); cwTestResult(lastDayOfMonth, lastSunday); } }and here is the test output:
g.
@ ciupazMI
Note: if you have a valid DateTime, let's name it MyDate, then instead of constants, you would simply write this:
if(LastDayOfMonth(MyDate.Year, MyDate.Month, out lastDayOfMonth)) { lastSunday = LastSunday(lastDayOfMonth); cwTestResult(lastDayOfMonth, lastSunday); }g.
Paul Linton
Star
13581 Points
2571 Posts
Re: Get the last Sunday of a month
Dec 19, 2012 04:44 AM|LINK
Gerry, the code in the original post uses a parameter to determine which weekday is required. So the original code will return the last Tuesday or Friday as required. Your longer, more complex code seems to be hardcoded to Sunday. You could simplify your code by creating an extension method as the original post asked for.
gerrylowry
All-Star
20577 Points
5721 Posts
Re: Get the last Sunday of a month
Dec 19, 2012 05:50 AM|LINK
@ Paul Linton
Hi Paul, based on this comment by the O.P.:
"from a datetime input parameter, gets me the last sunday of the month"
My solution is simple, short and not complex ... given the last day of the month, using existing .NET Framework capabilities, finding the last Sunday requires only two lines and no while loop:
meanwhile, computing the last day of the month is equally simple; given a year and a month, it simply uses the .DaysInMonth method to construct the end of the month date thanks to this constructor:
http://msdn.microsoft.com/en-us/library/xcfzdy4x.aspx
"DateTime Constructor (Int32, Int32, Int32)"
of course, the LastDayOfMonth looks longer because it allows for error processing.
SHORTER SPECIFIC VERSION -- 4 lines of code
public static DateTime LastSunday(DateTime validDate) { Int32 daysInThisMonth = DateTime.DaysInMonth(validDate.Year, validDate.Month); DateTime lastDayOfMonth = new DateTime(validDate.Year, validDate.Month, daysInThisMonth); Int32 dayValue = (Int32)lastDayOfMonth.DayOfWeek; return lastDayOfMonth.AddDays(-1*dayValue); }display method for testing:
public static void cwTestResult(DateTime testDate, DateTime lastSunday) { Console.WriteLine ("lastDayOfMonth {0}, lastSunday {1}", testDate.ToString("yyyy-MMMMM-dd dddd"), lastSunday.ToString("yyyy-MMMMM-dd dddd")); }output from the testing code:
and the code to perform the testing:
void Main() { DateTime testDate; DateTime lastSunday; testDate = new DateTime(2012, 1, 15); lastSunday = LastSunday(testDate); cwTestResult(testDate, lastSunday); testDate = new DateTime(2012, 2, 1); lastSunday = LastSunday(testDate); cwTestResult(testDate, lastSunday); testDate = new DateTime(2012, 3, 1); lastSunday = LastSunday(testDate); cwTestResult(testDate, lastSunday); testDate = new DateTime(2012, 12, 12); lastSunday = LastSunday(testDate); cwTestResult(testDate, lastSunday); }g.
Paul Linton
Star
13581 Points
2571 Posts
Re: Get the last Sunday of a month
Dec 19, 2012 05:53 AM|LINK