Saturday, September 25, 2010

How to calculate day difference

Demo:
Example:
Start Date               Duration              End Date
5/8/2010 9:00        4 Hrs                    5/8/2010 13:00
5/14/2010 13:00    4 Hrs                    5/14/2010 17:00
5/13/2010 9:00      8 Hrs                    5/13/2010 17:00
5/11/2010 13:00    8 Hrs                    5/12/2010 12:00
5/5/2010 9:00        12 Hrs                  5/6/2010 12:00
5/28/2010 13:00    12 Hrs                  5/29/2010 16:00
5/28/2010 13:00    16 Hrs                  5/30/2010 11:00
5/28/2010 13:00    20 Hrs                  5/30/2010 16:00
5/28/2010 9:00      20 Hrs                  5/30/2010 12:00
Discussion:
Code:
<%@ Page Language="C#" %>
<html>
<head id="Head1" runat="server">
    <script runat="server">
        private int startHour = 7;
        private int endHour = 19;
        private int workDaysPerWk = 5;
        protected override void OnLoad(EventArgs e)
        {
            this.lblEndTime.ForeColor = System.Drawing.Color.Green;
            this.lblEndTime.Text = string.Empty;
            try
            {
                this.startHour = int.Parse(this.txtStartHour.Text);
                this.endHour = int.Parse(this.txtEndHour.Text);
            }
            catch
            {
                this.lblEndTime.ForeColor = System.Drawing.Color.Red;
                this.lblEndTime.Text = "Error";
            }
            base.OnLoad(e);
            if (!this.IsPostBack)
            {
                DateTime now = DateTime.Now;
                if (now.DayOfWeek == DayOfWeek.Saturday) now = now.AddDays(2);
                if (now.DayOfWeek == DayOfWeek.Sunday) now = now.AddDays(1);
                this.calHoliday.SelectedDate = now;
                this.FindTimeIntervals();
            }
 
        }
        protected void FindTimeIntervals(object sender, EventArgs e)
        {
            this.FindTimeIntervals();
        }
        private void FindTimeIntervals()
        {
            DateTime date = this.calHoliday.SelectedDate;
            DateTime start = new DateTime(date.Year, date.Month, date.Day, startHour, 0, 0);
            this.ddlLeaveStart.Items.Clear();
            for (int i = 0; i < (endHour - startHour) * 2; i++)
                this.ddlLeaveStart.Items.Add(
                    new ListItem(start.AddMinutes(i * 30).ToString("yyyy MM dd - hh:mm tt"), 
                        start.AddMinutes(i * 30).ToString()));
        }
        protected void DayRender(object sender, DayRenderEventArgs e)
        {
            if (e.Day.IsWeekend)
                e.Cell.Enabled = false;
        }
        protected void FindStartTime(object sender, EventArgs e)
        {
            try
            {
                int workHours = endHour - startHour;
                DateTime end = DateTime.Parse(this.ddlLeaveStart.SelectedValue);
                int duration = int.Parse(this.txtDuration.Text);
                if (duration > workHours)
                {
                    int days = (duration - duration % workHours) / workHours;
                    duration = duration % workHours;
                    if (days > workDaysPerWk)
                    {
                        int weeks = (days - days % workDaysPerWk) / workDaysPerWk;
                        days = days % workDaysPerWk;
                        end = end.AddDays(weeks * 7);
                    }
                    end = end.AddDays(days);
                }
                end = end.AddHours(duration);
                if (end.Hour >= endHour || end.Hour < startHour) end = end.AddHours((24 - endHour) + startHour);
                if (end.DayOfWeek == DayOfWeek.Saturday || end.DayOfWeek == DayOfWeek.Sunday) end = end.AddDays(2);
                this.lblEndTime.Text = end.ToString("yyyy MM dd - hh:mm tt");
            }
            catch
            {
                this.lblEndTime.ForeColor = System.Drawing.Color.Red;
                this.lblEndTime.Text = "Invalid Input";
            }
        }
 
    </script>
</head>
<body>
    <form id="form1" runat="server">
        Start Hour: <asp:TextBox runat="server" ID="txtStartHour" Text="7" /> 
        End Hour: <asp:TextBox runat="server" ID="txtEndHour" Text="19" /> 
        <asp:Button runat="server" ID="btnSet" Text="Set" />
        <hr />
        Date: <asp:Calendar runat="server" ID="calHoliday" OnDayRender="DayRender" 
                                                OnSelectionChanged="FindTimeIntervals" />
        Time: <asp:DropDownList runat="server" ID="ddlLeaveStart"  />
        Duration: <asp:TextBox runat="server" ID="txtDuration" Text="12" />
        <asp:Button runat="server" ID="Find" OnClick="FindStartTime" Text="Find" />
        You should come back on: <asp:Label runat="server" ID="lblEndTime" />
    </form>
</body>
</html>

No comments:

Azure Storage Account Types

Defferent Types of Blobs Block blobs store text and binary data. Block blobs are made up of blocks of data that can be managed individually...