Click here to Skip to main content
15,918,168 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?

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;
        }

    }

}
 
Share this answer
 
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>
 
Share this answer
 
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    
HiCan 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