DayOfWeek/Date problem.
-
This is a weird one from a client. I've been racking my brains on how to do this, but as yet, no success. Starting from a random date, to another random date (school term dates), how can I flag up the second Tuesday of each calendar month? I will accept hoots and jeers :laugh:
-
This is a weird one from a client. I've been racking my brains on how to do this, but as yet, no success. Starting from a random date, to another random date (school term dates), how can I flag up the second Tuesday of each calendar month? I will accept hoots and jeers :laugh:
You could determine all the second Tuesdays for a year or two and then select the ones that fall within the target date range. Here are the second Tuesdays for 2014:
for ( int i = 0; i < 12; i++ ) {
DateTime beginDate = new DateTime( 2014, 01 + i, 01 ); int dayDiff = ( int ) DayOfWeek.Tuesday - ( int ) beginDate.DayOfWeek; DateTime secondTuesday = beginDate.AddDays( dayDiff + ( dayDiff < 0 ? 14 : 7 ) ); Console.WriteLine( "2nd Tuesday: {0}", secondTuesday ); } // end for.
-
This is a weird one from a client. I've been racking my brains on how to do this, but as yet, no success. Starting from a random date, to another random date (school term dates), how can I flag up the second Tuesday of each calendar month? I will accept hoots and jeers :laugh:
Well, the brute force method is to find the first Tuesday of a month and add 7 days. No, I'm not trying to be funny here, here's a simple way to achieve this:
public DateTime FindSecondTuesday(int month, int year)
{
int day = 1;
DateTime date = new DateTime(year, month, 1);
while (true)
{
if (date.DayOfWeek == DayOfWeek.Tuesday)
return date.AddDays(7);
date = date.AddDays(1);
}
}All you need do then, is call it for each month and year that you want to get the date from.
-
This is a weird one from a client. I've been racking my brains on how to do this, but as yet, no success. Starting from a random date, to another random date (school term dates), how can I flag up the second Tuesday of each calendar month? I will accept hoots and jeers :laugh:
I would take the essence of the other 2 solutions, apply ROW_NUMBER() partitioned over the date component and select the row number 2 for each month. Turn that into a view and select the record for a month/year
Never underestimate the power of human stupidity RAH
-
Well, the brute force method is to find the first Tuesday of a month and add 7 days. No, I'm not trying to be funny here, here's a simple way to achieve this:
public DateTime FindSecondTuesday(int month, int year)
{
int day = 1;
DateTime date = new DateTime(year, month, 1);
while (true)
{
if (date.DayOfWeek == DayOfWeek.Tuesday)
return date.AddDays(7);
date = date.AddDays(1);
}
}All you need do then, is call it for each month and year that you want to get the date from.
No need for a loop - just
switch
on theDayOfWeek
:public DateTime FindSecondTuesday(int month, int year)
{
DateTime date = new DateTime(year, month, 1);
switch (date.DayOfWeek)
{
case DayOfWeek.Sunday:
{
return date.AddDays(9);
}
case DayOfWeek.Monday:
{
return date.AddDays(8);
}
case DayOfWeek.Tuesday:
{
return date.AddDays(7);
}
case DayOfWeek.Wednesday:
{
return date.AddDays(13);
}
case DayOfWeek.Thursday:
{
return date.AddDays(12);
}
case DayOfWeek.Friday:
{
return date.AddDays(11);
}
case DayOfWeek.Saturday:
{
return date.AddDays(10);
}
default:
{
throw new InvalidOperationException("Lousy Smarch weather!");
}
}
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
This is a weird one from a client. I've been racking my brains on how to do this, but as yet, no success. Starting from a random date, to another random date (school term dates), how can I flag up the second Tuesday of each calendar month? I will accept hoots and jeers :laugh:
Thanks to everyone who answered my plea. - I've since found out that the meeting on the second Tuesday of the month is purely at one persons discretion, so I've decided to let that person enter the day on the timetable themselves. Thanks again, your answers were most appreciated.