Click here to Skip to main content
15,068,929 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Can some one help me to write a C# method to fetch the below shown format data in to a IEnumebrable .

I need to get the date range segregated along with the month also starting with Sunday and ends with Saturday.

MM   WeekNo Week_StartDate  Week_EndDate
------------------------------------------
01       1      01-JAN-13     05-JAN-13
01       2      06-JAN-13     12-JAN-13
01       3     13-JAN-13      19-JAN-13
01       4      20-JAN-13     26-JAN-13
01       5     27-JAN-13      31-JAN-13

02       1     01-FEB-13      02-FEB-13
02       2     03-FEB-13      09-FEB-13
02       3     10-FEB-13      16-FEB-13
02       4     17-FEB-13      23-FEB-13
02       5     24-FEB-13      28-FEB-13
Posted
Updated 12-Feb-15 23:23pm
v6
Comments
Tomas Takac 13-Feb-15 2:34am
   
What did you try so far? What is the source? Database? A file?
Anisuzzaman Sumon 13-Feb-15 4:13am
   
You question is not clear
RangappagariIT 13-Feb-15 5:13am
   
Sorry for not making it so clear.My source is two dates.When I pass the two dates ex: 01-Jan-2013 and 28Feb2013. I need to get the results segregated into weeks for each month as shown above. Week range should not contain the value for the previous month / Next month and Week should start by Sunday and should end by Saturday.
BillWoodruff 13-Feb-15 10:28am
   
It appears you have two accounts on CodeProject since this thread has a different author, but you appear to be speaking as the author.

Why is that ?
John C Rayan 13-Feb-15 6:08am
   
is it always the input dates are month start and month end?
RangappagariIT 13-Feb-15 6:21am
   
Thanks for you reply John. Those will be always input dates.The dates difference can be more than an year as well.
John C Rayan 13-Feb-15 6:39am
   
are they always month start and end dates for example 01/01/2015 to 31/01/2015 or 01/01/2015 to 31/01/2016. Do you understand my question? doesn't matter how apart they are.
RangappagariIT 13-Feb-15 7:22am
   
Those can be 31/10/2014 to 03/03/2015. The dates can be of any range. I had tried a solution and it works perfect. It would be helpful for me if you can help in making it a clean code as I have used multiple whiles and multiple ifs. here is my code might be helpful for others. Thanks :)
BillWoodruff 13-Feb-15 10:33am
   
Your desired output contains items per month which vary in structure: the first has a first entry with five days, followed by entries with six days; the second has a first entry with two days, followed by entries with six days.

Unless you specify what principle is involved in creating the variant items, how can we possibly assist you ?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using System.Collections;

namespace WeekRange
{
    class Program
    {
        static void Main(string[] args)
        {
            foreach (WeekRange wr in GetWeekRange(new DateTime(2015, 01, 05), new DateTime(2015, 04, 05)))
            {
                Console.WriteLine("{0} {1} {2} {3}", wr.WeekNo, wr.MM, wr.Start.Date.ToShortDateString(), wr.End.ToShortDateString());
            }
            Console.ReadLine();
        }

        public static IEnumerable<WeekRange> GetWeekRange(DateTime dtStart, DateTime dtEnd)
        {
            DateTime fWeekStart, dt, fWeekEnd;
            int wkCnt = 1;

            if (dtStart.DayOfWeek != DayOfWeek.Sunday)
            {
                fWeekStart = dtStart.AddDays(7 - (int)dtStart.DayOfWeek);
                fWeekEnd = fWeekStart.AddDays(-1);
                IEnumerable<WeekRange> ranges = getMonthRange(new WeekRange(dtStart, fWeekEnd, dtStart.Month, wkCnt++));
                foreach (WeekRange wr in ranges)
                {
                    yield return wr;
                }
                wkCnt = ranges.Last().WeekNo+1;

            }
            else
            {
                fWeekStart = dtStart;
            }


            for (dt = fWeekStart.AddDays(6); dt <= dtEnd; dt = dt.AddDays(7))
            {


                IEnumerable<WeekRange> ranges = getMonthRange(new WeekRange(fWeekStart, dt, fWeekStart.Month, wkCnt++));
                foreach (WeekRange wr in ranges)
                {
                    yield return wr;
                }
                wkCnt = ranges.Last().WeekNo + 1;
                fWeekStart = dt.AddDays(1);


            }

            if (dt > dtEnd)
            {

                IEnumerable<WeekRange> ranges = getMonthRange(new WeekRange(fWeekStart, dtEnd, dtEnd.Month, wkCnt++));
                foreach (WeekRange wr in ranges)
                {
                    yield return wr;
                }
                wkCnt = ranges.Last().WeekNo + 1;

            }

        }


        public static IEnumerable<WeekRange> getMonthRange(WeekRange weekRange)
        {

            List<WeekRange> ranges = new List<WeekRange>();

            if (weekRange.Start.Month != weekRange.End.Month)
            {
                DateTime lastDayOfMonth = new DateTime(weekRange.Start.Year, weekRange.Start.Month, 1).AddMonths(1).AddDays(-1);
                ranges.Add(new WeekRange(weekRange.Start, lastDayOfMonth, weekRange.Start.Month, weekRange.WeekNo));
                ranges.Add(new WeekRange(lastDayOfMonth.AddDays(1), weekRange.End, weekRange.End.Month, weekRange.WeekNo + 1));

            }
            else
            {
                ranges.Add(weekRange);
            }

            return ranges;

        }

    }


    struct WeekRange
    {
        public DateTime Start;
        public DateTime End;
        public int MM;
        public int WeekNo;

        public WeekRange(DateTime _start, DateTime _end, int _mm, int _weekNo)
        {
            Start = _start;
            End = _end;
            MM = _mm;
            WeekNo = _weekNo;
        }

    }

}
   
C#
public class WeekRange
    {
        public string Range { get; set; }
        public DateTime StartDate { get; set; }
        public DateTime EndDate { get; set; }
        public int Week { get; set; }
        public int Month { get; set; }
        public int Year { get; set; }
    }

        public List<weekrange> WeekDays(DateTime startDate, DateTime endDate)
        {
            DateTime startDateToCheck = startDate;
            DateTime dateToCheck = startDate;
            DateTime dateRangeBegin = dateToCheck;
            DateTime dateRangeEnd = endDate;

            List<weekrange> weekRangeList = new List<weekrange>();
            WeekRange weekRange = new WeekRange();


            while ((startDateToCheck.Year <= endDate.Year) && (startDateToCheck.Month <= endDate.Month) && dateToCheck <= endDate)
            {
                int week = 0;

                while (startDateToCheck.Month == dateToCheck.Month && dateToCheck <= endDate)
                {
                    week = week + 1;
                    dateRangeBegin = dateToCheck.AddDays(-(int)dateToCheck.DayOfWeek);
                    dateRangeEnd = dateToCheck.AddDays(6 - (int)dateToCheck.DayOfWeek);

                    if ((dateRangeBegin.Date < dateToCheck) && (dateRangeBegin.Date.Month != dateToCheck.Month))
                    {
                        dateRangeBegin = new DateTime(dateToCheck.Year, dateToCheck.Month, dateToCheck.Day);
                    }

                    if ((dateRangeEnd.Date > dateToCheck) && (dateRangeEnd.Date.Month != dateToCheck.Month))
                    {
                        DateTime dtTo = new DateTime(dateToCheck.Year, dateToCheck.Month, 1);
                        dtTo = dtTo.AddMonths(1);
                        dateRangeEnd = dtTo.AddDays(-(dtTo.Day));
                    }
                    if (dateRangeEnd.Date > endDate)
                    {
                        dateRangeEnd = new DateTime(dateRangeEnd.Year, dateRangeEnd.Month, endDate.Day);
                    }
                    weekRange = new WeekRange
                    {
                        StartDate = dateRangeBegin,
                        EndDate = dateRangeEnd,
                        Range = dateRangeBegin.Date.ToShortDateString() + '-' + dateRangeEnd.Date.ToShortDateString(),
                        Month = dateToCheck.Month,
                        Year = dateToCheck.Year,
                        Week = week
                    };
                    weekRangeList.Add(weekRange);
                    dateToCheck = dateRangeEnd.AddDays(1);
                }
                startDateToCheck = startDateToCheck.AddMonths(1);
            }

            return weekRangeList;
        }</weekrange></weekrange></weekrange>
   
v2
Comments
John C Rayan 13-Feb-15 7:35am
   
Thanks for your input. I'll see if I can send you a cleaner code soon
RangappagariIT 13-Feb-15 7:57am
   
Thanks John.
John C Rayan 13-Feb-15 17:59pm
   
Please see above solution 2, for my code
John C Rayan 16-Feb-15 6:42am
   
Hi Can you please accept the answer if you are happy with the solution.?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900