Calculate How Many Mondays in a Particular Month
-
Here's a method you could use...
int CountDaysInMonth(int month, int year, DayOfWeek dayToCount)
{
DateTime dt = new DateTime(year, month, 1);
int result = 0;
for(int i = 0; i < DateTime.DaysInMonth(year, month); i++)
{
if(dt.DayOfWeek == dayToCount)
result++;
dt.AddDays(1);
}
return result;
}Life goes very fast. Tomorrow, today is already yesterday.
-
Hello guys Can you please help me with this one? I want to make a mehtod that when you pass it a particular month and year it will return you the ammount of Mondays that are found in that Month of that particular day. Anyone has any ideas? Thank you in adavance Regards, Christian Pace
Obviously you do not need any loop or conditional for this. Have a look at:
int mondays=(DateTime.DaysInMonth(year, month) + (int)new DateTime(year, month, 6).DayOfWeek) / 7;
What remains of this homework is for you to figure out why the expression is what it is! :)
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
-
harold aptroot wrote:
it looks like The Right Way to do it
really? :)
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
-
harold aptroot wrote:
it looks like The Right Way to do it
really? :)
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
-
Obviously you do not need any loop or conditional for this. Have a look at:
int mondays=(DateTime.DaysInMonth(year, month) + (int)new DateTime(year, month, 6).DayOfWeek) / 7;
What remains of this homework is for you to figure out why the expression is what it is! :)
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
No, it's obvious that no loop is required, but I still have two conditionals in mine. I, like some of the others, am not content to hard code Monday, but would prefer to take the day of the week as a parameter. I haven't tried yours yet, but I don't see how it can be a general solution for any specified day of the week. On the whole, I expect the OP's teacher only wants to prove that he's smarter than his students.
-
No, it's obvious that no loop is required, but I still have two conditionals in mine. I, like some of the others, am not content to hard code Monday, but would prefer to take the day of the week as a parameter. I haven't tried yours yet, but I don't see how it can be a general solution for any specified day of the week. On the whole, I expect the OP's teacher only wants to prove that he's smarter than his students.
IMO it works for all years and months, I checked for this year only. With a day-of-week parameter it becomes:
int Xdays=(DateTime.DaysInMonth(year, month) + (int)new DateTime(year, month, 7-dayOfWeek).DayOfWeek) / 7;
where dayOfWeek is 0=SU, 1=MO, ... 6=SA [ADDED] so you can also use (int)DayOfWeek [/ADDED] :)
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
modified on Wednesday, November 18, 2009 3:36 PM
-
No, it's obvious that no loop is required, but I still have two conditionals in mine. I, like some of the others, am not content to hard code Monday, but would prefer to take the day of the week as a parameter. I haven't tried yours yet, but I don't see how it can be a general solution for any specified day of the week. On the whole, I expect the OP's teacher only wants to prove that he's smarter than his students.
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
-
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.