here is a C# code to demonstrate business hour calculation. I have developed it after consulting and tweaking various scraps of code on the web so appreciate all of those people
the aspx page
———————-
<%@ Page Language=”C#” AutoEventWireup=”true” CodeBehind=”TestDateFunctions.aspx.cs” Inherits=”TestWebApplication.TestDateFunctions” %>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd“>
<html xmlns=”http://www.w3.org/1999/xhtml” >
<head runat=”server”>
<title>Untitled Page</title>
</head>
<body>
<form id=”form1″ runat=”server”>
<div>
<asp:Label ID=”Label2″ runat=”server” Text=”Year”></asp:Label>
<asp:DropDownList ID=”ddyr” runat=”server”> </asp:DropDownList>
<asp:Label ID=”Label3″ runat=”server” Text=”Month”></asp:Label>
<asp:DropDownList ID=”ddmo” runat=”server”> </asp:DropDownList>
<asp:Label ID=”Label4″ runat=”server” Text=”Date”></asp:Label>
<asp:DropDownList ID=”ddda” runat=”server”> </asp:DropDownList>
<asp:Label ID=”Label5″ runat=”server” Text=”Hour”></asp:Label>
<asp:DropDownList ID=”ddho” runat=”server”> </asp:DropDownList>
<asp:Label ID=”Label7″ runat=”server” Text=”Minute”></asp:Label>
<asp:DropDownList ID=”ddmi” runat=”server”> </asp:DropDownList>
<asp:Label ID=”Label6″ runat=”server” Text=”sec”></asp:Label>
<asp:DropDownList ID=”ddse” runat=”server”> </asp:DropDownList>
<asp:Button ID=”Button1″
runat=”server” Text=”Button” onclick=”Button1_Click” /></br> <asp:Label ID=”Label1″ runat=”server” Text=”Label”></asp:Label>
</div>
</form>
</body>
</html>
——————-
the c# code
—————————-
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Globalization;
namespace TestWebApplication
{
public partial class TestDateFunctions : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = DateTime.Now.ToString();
if (!IsPostBack)
{
for (int yr = 1900; yr <= 2100; yr++)
{ ddyr.Items.Add(yr.ToString()); }
for (int mo = 1; mo <= 12; mo++)
{ ddmo.Items.Add(mo.ToString()); }
for (int da = 1; da <= 31; da++)
{ ddda.Items.Add(da.ToString()); }
for (int hr = 0; hr <= 23; hr++)
{ ddho.Items.Add(hr.ToString()); }
for (int mi = 0; mi <= 59; mi++)
{ ddmi.Items.Add(mi.ToString()); }
for (int se = 0; se <= 59; se++)
{ ddse.Items.Add(se.ToString()); }
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text=GetCaseOwner();
}
public string GetCaseOwner()
{
string caseowner = “Me”;
DateTime rightNow ;
rightNow = new DateTime(int.Parse(ddyr.SelectedValue), int.Parse(ddmo.SelectedValue), int.Parse(ddda.SelectedValue), int.Parse(ddho.SelectedValue), int.Parse(ddmi.SelectedValue), int.Parse(ddse.SelectedValue));
int dayNow = (int)rightNow.DayOfWeek; //0 is sunday and 6 is staurday
int timeHourNow = int.Parse(rightNow.Hour.ToString()); // 24 hour format
int timeMonth = (int)rightNow.Month;
int timeDateofMonth = int.Parse(rightNow.Date.ToString(“dd”));
if (!IsHoliday(rightNow))// market holiday
{
if ((dayNow == 0 || dayNow == 6) && timeHourNow >= 7 && timeHourNow < 16 )//Weekend hours
{
caseowner = “You”;
}
else if ((dayNow != 0 || dayNow != 6) && timeHourNow >= 6 && timeHourNow < 21) //WeekDay hours
{
caseowner = “You”;
}
}
return caseowner;
}
// private string WhichDay(string weekdayNumber, string monthNumber,string DayInMonthNumber, string TimeIntHour, string WeekIntOfMonth)
private bool IsHoliday(DateTime findDate)
{
string whichDay = “”;
int dayNow = (int)findDate.DayOfWeek; //0 is sunday and 6 is staurday
int monthNumber = (int)findDate.Month;
int DayInMonthNumber = int.Parse(findDate.Date.ToString(“dd”));
string dt = findDate.ToShortDateString();
if (monthNumber == 1 && DayInMonthNumber == 1)
{
whichDay = “NewYear”;
}
// else if(monthNumber==”3″ && weekdayNumber==”1″)
else if (dt == DayOfTheMonth(1, System.DayOfWeek.Monday, 3).ToShortDateString())
{
whichDay = “MLKD”;
}
else if (dt == DayOfTheMonth(2, System.DayOfWeek.Monday, 3).ToShortDateString())
{
whichDay = “Presidents”;
}
else if (monthNumber == 7 && DayInMonthNumber == 4)
{
whichDay = “Idependence”;
}
else if (dt == DayOfTheMonth(9, System.DayOfWeek.Monday, 1).ToShortDateString())
{
whichDay = “Labor”;
}
else if (monthNumber == 12 && DayInMonthNumber == 25)
{
whichDay = “Christmas”;
}
else if (dt == LastDayOfTheMonth(11, System.DayOfWeek.Thursday).ToShortDateString())
{
whichDay = “ThanksGiving”;
}
else if (dt == LastDayOfTheMonth(5, System.DayOfWeek.Monday).ToShortDateString())
{
whichDay = “Memorial”;
}
else if (dt == EasterSunday(DateTime.Now.Year).AddDays(-2).ToShortDateString())
{
whichDay = “GoodFriday”;
}
if (whichDay == “”)
return false;
else
return true;
}
private DateTime LastDayOfTheMonth(int Month, System.DayOfWeek DayOfTheWeek)
{
return LastDayOfTheMonth(Month, DateTime.Today.Year, DayOfTheWeek);
}
private DateTime LastDayOfTheMonth(int Month, int Year, System.DayOfWeek DayOfTheWeek)
{
System.Globalization.Calendar MyCalendar = CultureInfo.CurrentCulture.Calendar;
int NoOfDays = MyCalendar.GetDaysInMonth(Year, Month);
DateTime MyDate = new DateTime(Year, Month, NoOfDays);
while (MyDate.DayOfWeek != DayOfTheWeek)
{
MyDate = MyDate.AddDays(-1);
}
return MyDate;
}
private DateTime DayOfTheMonth(int Month, System.DayOfWeek DayOfTheWeek, int weekNumber)
{
return DayOfTheMonth(Month, DateTime.Today.Year, DayOfTheWeek, weekNumber);
}
private DateTime DayOfTheMonth(int Month, int Year, System.DayOfWeek DayOfTheWeek, int weekNumber) //weeknumber 1= first, 0 = last , 2,3,4,5 are specific weeknumbers
{
System.Globalization.Calendar MyCalendar = CultureInfo.CurrentCulture.Calendar;
int NoOfDays = MyCalendar.GetDaysInMonth(Year, Month);
DateTime MyDate = new DateTime(Year, Month, 1);
// int nthDayOfMonth = 1;
if (weekNumber == 0) //LastDay
{
MyDate = new DateTime(Year, Month, NoOfDays);
while (MyDate.DayOfWeek != DayOfTheWeek)
{
MyDate = MyDate.AddDays(-1);
}
}
if (weekNumber >= 1) //FirstDay
{
MyDate = new DateTime(Year, Month, 1);
for (int i = 1; i <= weekNumber; i++)
{
if (i != 1) { MyDate = MyDate.AddDays(1); }
while (MyDate.DayOfWeek != DayOfTheWeek)
{
MyDate = MyDate.AddDays(1);
}
}
}
return MyDate;
}
private DateTime EasterSunday(int year)
{
int day = 0;
int month = 0;
int g = year % 19;
int c = year / 100;
int h = (c – (int)(c / 4) – (int)((8 * c + 13) / 25) + 19 * g + 15) % 30;
int i = h – (int)(h / 28) * (1 – (int)(h / 28) * (int)(29 / (h + 1)) * (int)((21 – g) / 11));
day = i – ((year + (int)(year / 4) + i + 2 – c + (int)(c / 4)) % 7) + 28;
month = 3;
if (day > 31)
{
month++;
day -= 31;
}
return new DateTime(year, month, day);
}
}
}
———————————
Leave a comment