Calculate How Many Mondays in a Particular Month
-
Improved...
int CountDaysInMonth(int month, int year, DayOfWeek dayToCount)
{
DateTime dt = new DateTime(year, month, 1);
int firstInstance = 1;
if((int)dt.DayOfWeek > (int)dayToCount)
firstInstance += (7 - (int)dt.DayOfWeek) + (int)dayToCount;
else if((int)dt.DayOfWeek < (int)dayToCount)
firstInstance += (int)dayToCount - (int)dt.DayOfWeek;
return firstInstance <= (DateTime.DaysInMonth(month, year) - 28) ? 5 : 4;
}...I think the logic is right. [EDIT] That was way off, I fixed now... I think!
Life goes very fast. Tomorrow, today is already yesterday.
What I came up with is similar to that:
public static int
F
(
int Year
,
int Month
,
System.DayOfWeek DayOfWeek
)
{
int d = (int) DayOfWeek - (int) (new System.DateTime ( Year , Month , 1 )).DayOfWeek ;if ( d < 0 ) { d += 7 ; } int n = System.DateTime.DaysInMonth ( Year , Month ) - 28 ; return ( d < n ? 5 : 4 ) ;
}
-
PIEBALDconsult wrote:
I expect the OP's teacher only wants to prove that he's smarter than his students
The teacher may be shaded then by a cheating student. :laugh:
Luc Pattyn [Forum Guidelines] [My Articles]
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Lol guys Thank you very much for the help ;) but actually this ain't for homework hehe, this is part of a payroll system that I am developing at work and with the deadline stress and from being tired because been programming 8 hours strait i couldn't find a solution for this so i turned to some help ;) hehe thank you again guys.