Work days count in Month...

Hariinakoti
Posted by Hariinakoti under ASP.NET category on | Points: 40 | Views : 2174
IFormatProvider provider = new System.Globalization.CultureInfo("en-CA", true);
String datetime = txtdate.Text.Trim();
DateTime dt = DateTime.Parse(datetime, provider, System.Globalization.DateTimeStyles.NoCurrentDateDefault);
string Sday = Convert.ToDateTime(dt).DayOfWeek.ToString();
int sundy = 0;
int Cols = System.DateTime.DaysInMonth(Convert.ToDateTime(dt).Year, Convert.ToDateTime(dt).Month) + 3;
//MessageBox.Show("days.."+Cols);
int[] sundays = new int[5];
sundays[0] = 0; sundays[1] = 0; sundays[2] = 0; sundays[3] = 0; sundays[4] = 0;
// weekdays Weekday = (weekdays)(Sday);
switch (Sday)
{
case "Sunday":
sundy = Convert.ToDateTime(dt).Day;
break;
case "Saturday":
sundy = Convert.ToDateTime(dt).Day + 1;

break;
case "Friday":
sundy = Convert.ToDateTime(dt).Day + 2;
break;
case "Thursday":
sundy = Convert.ToDateTime(dt).Day + 3;
break;
case "Wednesday":
sundy = Convert.ToDateTime(dt).Day + 4;
break;
case "Tuesday":
sundy = Convert.ToDateTime(dt).Day + 5;
break;
case "Monday":
sundy = Convert.ToDateTime(dt).Day + 6;
break;
}

sundays[0] = sundy;
if (sundays[0] + 7 > System.DateTime.DaysInMonth(Convert.ToDateTime(dt).Year, Convert.ToDateTime(dt).Month))
{
sundays[1] = sundays[0] - 7;
sundays[2] = sundays[1] - 7;
sundays[3] = sundays[2] - 7;
sundays[4] = sundays[3] - 7;
}
else
{
if (sundays[0] + 7 <= System.DateTime.DaysInMonth(Convert.ToDateTime(dt).Year, Convert.ToDateTime(dt).Month))
{
sundays[1] = sundays[0] + 7;
if (sundays[1] + 7 > System.DateTime.DaysInMonth(Convert.ToDateTime(dt).Year, Convert.ToDateTime(dt).Month))
{
sundays[2] = sundays[1] - 14;
sundays[3] = sundays[2] - 7;
sundays[4] = sundays[3] - 7;
}
else
{
if (sundays[1] + 7 <= System.DateTime.DaysInMonth(Convert.ToDateTime(dt).Year, Convert.ToDateTime(dt).Month))
{
sundays[2] = sundays[1] + 7;
if (sundays[2] + 7 > System.DateTime.DaysInMonth(Convert.ToDateTime(dt).Year, Convert.ToDateTime(dt).Month))
{
sundays[3] = sundays[2] - 21;
sundays[4] = sundays[3] - 7;
}
else
{
if (sundays[2] + 7 <= System.DateTime.DaysInMonth(Convert.ToDateTime(dt).Year, Convert.ToDateTime(dt).Month))
{
sundays[3] = sundays[2] + 7;
if (sundays[3] + 7 > System.DateTime.DaysInMonth(Convert.ToDateTime(dt).Year, Convert.ToDateTime(dt).Month))
{
sundays[4] = sundays[3] - 28;
}
else
{
sundays[4] = sundays[3] + 7;
}
}
}
}
}
}
}

int scount = 0;
int day = Convert.ToDateTime(dt).Day;
for (int i = 0; i < 5; i++)
{
if (sundays[i] > 0)
{
scount++;
}
}
int twd;
int month = Convert.ToDateTime(dt).Month;
int year = Convert.ToDateTime(dt).Year;
try
{
int day1 = System.DateTime.DaysInMonth(Convert.ToDateTime(dt).Year, Convert.ToDateTime(dt).Month);
//MessageBox.Show("month=" + month + " day=" + day);
query1 = "select count(*) from HolidayList where Date in (select Date from HolidayList where month(convert(datetime,Date,103))=" + month + " and year(convert(datetime,Date,103))=" + year + ")";
int holiday = int.Parse(dal.Executescalard(query1));
query1 = "select count(*) from Extra_Days where Date in (select Date from Extra_Days where month(convert(datetime,Date,103))=" + month + " and year(convert(datetime,Date,103))=" + year + ")";
int Eday = int.Parse(dal.Executescalard(query1));
//int j = dal.Executereaderd(query1);
//int hdays = int.Parse(read.GetValue(0).ToString());
twd = (day1 - (holiday + scount)) + Eday;
txtwd.Text = twd.ToString();
}
catch (Exception ex)
{
lblmsg.Text = ex.Message;
lblmsg.Visible = true;
lblmsg.ForeColor = System.Drawing.Color.Red;
}

Comments or Responses

Login to post response