Search This Blog

Monday, October 8, 2012

Find out week in month find week range

Query: How can find out week in month find week range.



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SBIWEB
{
    public partial class weekly : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        private string getDatetm(DateTime dt)
        {

            string s = string.Empty;
            DateTime dtTempS = dt;
            DateTime dtTempL = dt;
            int daysLeft = 7;
            int totalDayRemaing = DateTime.DaysInMonth(dt.Year, dt.Month);
            if ((int)dt.DayOfWeek != 1)
            {
                daysLeft = (int)dt.DayOfWeek;
                if (daysLeft == 0)
                    daysLeft = 8;
                daysLeft = 7 - daysLeft + 1;
            }
            while (totalDayRemaing > 7)
            {
                dtTempL = dtTempS.AddDays(daysLeft - 1);
                s = s + dtTempS.ToString("dd/MM") + " - " + dtTempL.ToString("dd/MM") + "\r\n";
                dtTempS = dtTempL.AddDays(1);
                totalDayRemaing = totalDayRemaing - (daysLeft);
                daysLeft = 7;
            }
            dtTempL = dtTempS.AddDays(totalDayRemaing - 1);
            s = s + dtTempS.ToString("dd/MM") + " - " + dtTempL.ToString("dd/MM") + "\r\n";
            return s;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string s = string.Empty;
            DateTime dtStart = new DateTime(2012, 3, 1);
            DateTime dtEnd = new DateTime(2013, 4, 30);
            while (dtStart.Year < dtEnd.Year || dtStart.Month <= dtEnd.Month)
            {
                s = s + getDatetm(dtStart);
                dtStart = dtStart.AddMonths(1);
            }
            Response.Write(HttpUtility.HtmlEncode(s).Replace("\n", "<br />"));
        }
    }
}

Output:

01/03 - 04/03
05/03 - 11/03
12/03 - 18/03
19/03 - 25/03
26/03 - 31/03 

No comments :