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
Hi all,
I hope most of the beginers could encounter same issues as mentioned above. Even i too have tried installing MS CRM 4.0 on Win 2003 R2 Server.
Thanx for sharing in advance…..
hi
— This funtion retutns true if Second Saturday else false
create function [dbo].[IsSecondSaturday ]( @date as datetime
)returns bit
as
begin
if DATENAME(weekday,@date) = ‘Saturday’ and datepart (dd,@date) between 7and 15
return 1 — if Second Saturday
return 0
end
Hi Chirag Darji
As per your above code
Pls Tell me how to know 2nd and fourth Saturday
how many types of bindings in .net?
darjibhai, kya khovai gaya?
thanks….
I am looking for same problem. i have to disable 2 nd and 4th sat in calender as a holiday..
dasdasdasd
[…] See https://chiragrdarji.wordpress.com/2010/08/23/find-second-saturday-and-fourth-saturday-of-month/ […]