Does anyone have the script that returns that second last sunday of a month?

  • Thread starter Thread starter TaeHo Yoo
  • Start date Start date
T

TaeHo Yoo

I have a scheduling function and in that function I want to excute
something if today is the second last sunday of a month.

Cheers
 
Well, you can convert this C++ code to get the first Sunday of the month
and then work out the math for a specific Sunday of the month given the
days in the month. Full C++ code at:

http://www.geocities.com/jeff_louie/date.htm

// Days since Sunday 0-6
// Based on Zeller
// ASSERT Year>1, Month>=1 && <=12.
int CCalendar::GetFirstDayInMonth(unsigned int Year, unsigned int
Month)
{
    const int day=1;
    if (Month < 3) {
        Month +=12;
        Year -= 1;
    }
    return ((day+1+(Month*2)+(int)((Month+1)*3/5)+Year+(int)(Year/4)-
(int)(Year/100)+(int)(Year/400))%7);
}

Regards,
Jeff
I have a scheduling function and in that function I want to excute
something if today is the second last sunday of a month.<
 
I have a scheduling function and in that function I want to excute
something if today is the second last sunday of a month.

Off the top of my head, the easiest thing here would be to check
if the next sunday is still in the same month, but two weeks from
the date in question is not...

private bool CheckSecondLastSunday()
{

DateTime dt = DateTime.Now;

if( (dt.DayOfWeek == DayOfWeek.Sunday) &&
(dt.AddDays(7).Month == dt.Month) &&
(dt.AddDays(14).Month != dt.Month) )

return true;
else
return false;

}
 
Back
Top