DateTime periodStart = Convert.ToDateTime(txtStartingDate.Text);
DateTime periodEnd = Convert.ToDateTime(txtEndDate.Text);
const DayOfWeek FIRST_DAY_OF_WEEK = DayOfWeek.Monday;
const DayOfWeek LAST_DAY_OF_WEEK = DayOfWeek.Sunday;
const int DAYS_IN_WEEK = 7;
DateTime firstDayOfWeekBeforeStartDate;
int daysBetweenStartDateAndPreviousFirstDayOfWeek = (int)periodStart.DayOfWeek - (int)FIRST_DAY_OF_WEEK;
if (daysBetweenStartDateAndPreviousFirstDayOfWeek >= 0)
{
firstDayOfWeekBeforeStartDate = periodStart.AddDays(-daysBetweenStartDateAndPreviousFirstDayOfWeek);
}
else
{
firstDayOfWeekBeforeStartDate = periodStart.AddDays(-(daysBetweenStartDateAndPreviousFirstDayOfWeek + DAYS_IN_WEEK));
}
DateTime lastDayOfWeekAfterEndDate;
int daysBetweenEndDateAndFollowingLastDayOfWeek = (int)LAST_DAY_OF_WEEK - (int)periodEnd.DayOfWeek;
if (daysBetweenEndDateAndFollowingLastDayOfWeek >= 0)
{
lastDayOfWeekAfterEndDate = periodEnd.AddDays(daysBetweenEndDateAndFollowingLastDayOfWeek);
}
else
{
lastDayOfWeekAfterEndDate = periodEnd.AddDays(daysBetweenEndDateAndFollowingLastDayOfWeek + DAYS_IN_WEEK);
}
int calendarWeeks = 1 + (int)((lastDayOfWeekAfterEndDate - firstDayOfWeekBeforeStartDate).TotalDays / DAYS_IN_WEEK);
if (calendarWeeks == 1)
{
DateTime startDate = Convert.ToDateTime(txtStartingDate.Text);
DateTime endDate = Convert.ToDateTime(txtEndDate.Text);
TimeSpan ts = endDate.Subtract(startDate);
TimeSpan timespan = Convert.ToDateTime(txtEndDate.Text).Subtract(Convert.ToDateTime(txtStartingDate.Text));
string TimeSpan1 = timespan.Days.ToString();
int Final = Convert.ToInt16(TimeSpan1) + 1;
int i = obj.FireQuery("INSERT INTO Leave (intEmpId,LeaveType,LeaveReason,StartingDate,EndingDate,LeaveStatus,LeaveDays,CreatedDate,ApprovedBy) VALUES (7,'" + drpLeaveType.SelectedItem.Text + "','" + txtLeaveReason.Text + "','" + Convert.ToDateTime(txtStartingDate.Text) + "','" + Convert.ToDateTime(txtEndDate.Text) + "','Pending','" + Final.ToString() + "','" + DateTime.Now + "',1)");
}
else
{
TimeSpan timespan = Convert.ToDateTime(txtEndDate.Text).Subtract(Convert.ToDateTime(txtStartingDate.Text));
//int Time = Convert.ToInt16(Convert.ToDateTime(txtEndDate.Text).Subtract(Convert.ToDateTime(txtStartingDate.Text)));
int timeIncrease = Convert.ToInt16(timespan.Days.ToString()) + 1;
string timeString = timespan.Days.ToString();
int Final = timeIncrease - calendarWeeks + 1;
int i = obj.FireQuery("INSERT INTO Leave (intEmpId,LeaveType,LeaveReason,StartingDate,EndingDate,LeaveStatus,LeaveDays,CreatedDate,ApprovedBy) VALUES (7,'" + drpLeaveType.SelectedItem.Text + "','" + txtLeaveReason.Text + "','" + Convert.ToDateTime(txtStartingDate.Text) + "','" + Convert.ToDateTime(txtEndDate.Text) + "','Pending','" + Final.ToString() + "','" + DateTime.Now + "',1)");
}
}
No comments:
Post a Comment