Monday, August 23, 2010

Find Second Saturday and Fourth Saturday of Month

       While analyzing Payroll management system, I came across an interesting question that how can we find second and fourth Saturday of every month. As in India we still do not have five days a week in most of organization, bad humm……:( . Most of organizations offer only second and fourth Saturday off. Below is the code to find particular day of week and its occurrence in given month and year,

/// <summary>


/// Returns date for specific week day and its occurrence in month.


/// </summary>


/// <param name="DesiredDay">Perticular day of week, System.DayOfWeek</param>


/// <param name="Occurrence">Its occurrence, System.Int32</param>


/// <param name="Month">Month, System.Int32</param>


/// <param name="Year">Year, System.Int32</param>


/// <returns></returns>


private int GetDateForWeekDay(DayOfWeek DesiredDay, int Occurrence,int Month,int Year)


{


    


    DateTime dtSat = new DateTime(Year, Month , 1);


    int j = 0;


    if (Convert.ToInt32(DesiredDay) - Convert.ToInt32(dtSat.DayOfWeek) >= 0)


        j = Convert.ToInt32(DesiredDay) - Convert.ToInt32(dtSat.DayOfWeek) + 1;


    else


        j = (7 - Convert.ToInt32(dtSat.DayOfWeek)) + (Convert.ToInt32(DesiredDay) + 1);


 


    return j + (Occurrence - 1) * 7;


 


    


}




Fig - (1) Code to find Second Saturday and Fourth Saturday of Month.



Happy Programming !!!!

Chirag Darji


ASP.NET Consultant & Trainer

No comments: