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

2 comments:

Aishwarya said...

I’m really amazed with your posting skills as well as with the layout on your blog site. Is this a paid style or did you modify it yourself? Either way keep up the pleasant quality writing, it is rare to see a great site such as this one these days.
ERP Software Companies in Chennai
ERP Software In India
ERP Companies In Chennai
ERP Software In Chennai

Cleaning houses said...


Furniture Moving
Insect Control *Insulation * inspection Villas*Water leak detection * Cleaning Villas*Detection of water leaks*Moving furniture in Jazan*Cleaning apartments in Jizan