locked
Need to get monthwise working days between a given start date and end date RRS feed

  • Question

  • User-127625424 posted

    I need to write a method where i will pass a start date and a end date . The output should be a list with two parametsrs . One is month name and other is no of working days in that month.  (removing sat and sun)

    Please advise.

    Wednesday, October 15, 2014 7:18 AM

Answers

  • User-271186128 posted

    Hi RRam,


    But i have start date and end from different months and in the output i need a list which has splitup of working days for every month.

    According to your description, I have created a sample, you could refer to it.

            protected void btnOutPut_Click(object sender, EventArgs e)
            {
                DateTime fromdate = Convert.ToDateTime(txtFromDate.Text);
                DateTime endDate = Convert.ToDateTime(txtEndDate.Text);
               
                StringBuilder sb = new StringBuilder();
    
                //In the same Year.
                if (fromdate.Year == endDate.Year)
                {
                    //In the same Year and Month
                    if (fromdate.Month == endDate.Month)
                    {
                        int count1 = 0;
                        DateTime dt1 = fromdate;
                        for (int i = fromdate.Day; i <= endDate.Day; i++)
                        {
                            if (dt1.DayOfWeek != DayOfWeek.Saturday && dt1.DayOfWeek != DayOfWeek.Sunday)
                            {
                                count1++;
                            }
                            dt1 = dt1.AddDays(1);
                        }
                        sb.AppendLine(string.Format("Month:" + fromdate.Month + "  " + "Days:" + count1));
                    }
                    else if (fromdate.Month < endDate.Month)
                    {
                        int count2 = 0;
                        //
                        DateTime dt1 = fromdate;
                        for (int i = fromdate.Day; i <= DateTime.DaysInMonth(fromdate.Year,fromdate.Month); i++)
                        {
                            if (dt1.DayOfWeek != DayOfWeek.Saturday && dt1.DayOfWeek != DayOfWeek.Sunday)
                            {
                                count2++;
                            }
                            dt1 = dt1.AddDays(1);
                        }
                        sb.AppendLine(string.Format("Month:" + fromdate.Month + "  " + "Days:" + count2));
    
                        //
                        DateTime mm = fromdate.AddMonths(1);
                        for (int i = mm.Month; i < endDate.Month; i++)
                        {
                            int count3 = 0;
                            DateTime dt2 = new DateTime(mm.Year, mm.Month, 1);
                            for (int j = 1; j <= DateTime.DaysInMonth(mm.Year, mm.Month); j++)
                            {
                                if (dt2.DayOfWeek != DayOfWeek.Saturday && dt2.DayOfWeek != DayOfWeek.Sunday)
                                {
                                    count3++;
                                }
                                dt2 = dt2.AddDays(1);
                            }
                            sb.AppendLine(string.Format("Month:" + mm.Month + "  " + "Days:" + count3));
    
                            mm = mm.AddMonths(1);
                        }
    
                        //
                        int count4 = 0;
                        DateTime dt3 = new DateTime(endDate.Year, endDate.Month, 1);
                        for (int i = dt3.Day; i <= endDate.Day; i++)
                        {
                            if (dt3.DayOfWeek != DayOfWeek.Saturday && dt3.DayOfWeek != DayOfWeek.Sunday)
                            {
                                count4++;
                            }
                            dt3 = dt3.AddDays(1);
                        }
                        sb.AppendLine(string.Format("Month:" + dt3.Month + "  " + "Days:" + count4));
                    }
                    else
                    {
                        Response.Write("<script> alert('please enter a former date');</script>");
                    }
                }
                else
                {
                    //Please refer to the above code.
                }            
            }

    If you have any other questions about my reply, please let me know freely.

    Best Regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 16, 2014 6:51 AM
  • User487807879 posted

    This will get you what you want, but you will see only number of working days between start and end date. So, if you enter 10/25/2014 as a start date, you will get 5 as a number of working days. If you want to get complete number of working days for the whole month, you will have to modify the code.

        protected void Page_Load(object sender, EventArgs e)
        {
            foreach (MonthDaysData lobj_Data in GetWorkingDays(new DateTime(2014, 5, 2), new DateTime(2014, 10, 18)))
            {
                Response.Write(lobj_Data.Month.ToString() + "/" + lobj_Data.Year.ToString() + ": " + lobj_Data.WorkingDays.ToString() + "<br/>");
            }
        }
    
        private List<MonthDaysData> GetWorkingDays(DateTime ldt_DateStart, DateTime ldt_DateEnd)
        {
            List<MonthDaysData> lobj_MonthDaysDataList = new List<MonthDaysData>();
            while (ldt_DateEnd.Date > ldt_DateStart.Date)
            {
                if (ldt_DateStart.DayOfWeek != DayOfWeek.Saturday && ldt_DateStart.DayOfWeek != DayOfWeek.Sunday)
                {
                    MonthDaysData lobj_MonthDaysData = lobj_MonthDaysDataList.FirstOrDefault(o => o.Month == ldt_DateStart.Month && o.Year == ldt_DateStart.Year);
                    if (lobj_MonthDaysData == null)
                    {
                        lobj_MonthDaysData = new MonthDaysData()
                        {
                            Year = ldt_DateStart.Year,
                            Month = ldt_DateStart.Month,
                            WorkingDays = 1
                        };
                        lobj_MonthDaysDataList.Add(lobj_MonthDaysData);
                    }
                    else
                    {
                        lobj_MonthDaysData.WorkingDays += 1;
                    }
                }
                ldt_DateStart = ldt_DateStart.AddDays(1);
            }
            return lobj_MonthDaysDataList;
        }
    
        public class MonthDaysData
        {
            public int Year { get; set; }
            public int Month { get; set; }
            public int WorkingDays { get; set; }
        }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 16, 2014 9:30 AM

All replies

  • User487807879 posted

    Will those dates you pass into the function be from the same month? In any case, this method will return number of working days between start and end date:

        private int GetWorkingDays(DateTime ldt_DateStart, DateTime ldt_DateEnd)
        {
            int li_RetVal = 0;
            while (ldt_DateEnd.Date > ldt_DateStart.Date)
            {
                if (ldt_DateStart.DayOfWeek != DayOfWeek.Saturday && ldt_DateStart.DayOfWeek != DayOfWeek.Sunday)
                {
                    li_RetVal++;
                }
                ldt_DateStart = ldt_DateStart.AddDays(1);
            }
            return li_RetVal;
        }

    Wednesday, October 15, 2014 8:58 AM
  • User-127625424 posted

    thanks Kipo for your answer. But i have start date and end from different months and in the output i need a list which has splitup of working days for every month.

    please advice

    public List<MonthDaysData> GetMonthwiseWorkingdays(DateTime? start, DateTime? end)
    {
    List<MonthDaysData> monthdays = new List<MonthDaysData>();

    // Coding to get the output
    return monthdays;
    }

    Thursday, October 16, 2014 2:23 AM
  • User-271186128 posted

    Hi RRam,


    But i have start date and end from different months and in the output i need a list which has splitup of working days for every month.

    According to your description, I have created a sample, you could refer to it.

            protected void btnOutPut_Click(object sender, EventArgs e)
            {
                DateTime fromdate = Convert.ToDateTime(txtFromDate.Text);
                DateTime endDate = Convert.ToDateTime(txtEndDate.Text);
               
                StringBuilder sb = new StringBuilder();
    
                //In the same Year.
                if (fromdate.Year == endDate.Year)
                {
                    //In the same Year and Month
                    if (fromdate.Month == endDate.Month)
                    {
                        int count1 = 0;
                        DateTime dt1 = fromdate;
                        for (int i = fromdate.Day; i <= endDate.Day; i++)
                        {
                            if (dt1.DayOfWeek != DayOfWeek.Saturday && dt1.DayOfWeek != DayOfWeek.Sunday)
                            {
                                count1++;
                            }
                            dt1 = dt1.AddDays(1);
                        }
                        sb.AppendLine(string.Format("Month:" + fromdate.Month + "  " + "Days:" + count1));
                    }
                    else if (fromdate.Month < endDate.Month)
                    {
                        int count2 = 0;
                        //
                        DateTime dt1 = fromdate;
                        for (int i = fromdate.Day; i <= DateTime.DaysInMonth(fromdate.Year,fromdate.Month); i++)
                        {
                            if (dt1.DayOfWeek != DayOfWeek.Saturday && dt1.DayOfWeek != DayOfWeek.Sunday)
                            {
                                count2++;
                            }
                            dt1 = dt1.AddDays(1);
                        }
                        sb.AppendLine(string.Format("Month:" + fromdate.Month + "  " + "Days:" + count2));
    
                        //
                        DateTime mm = fromdate.AddMonths(1);
                        for (int i = mm.Month; i < endDate.Month; i++)
                        {
                            int count3 = 0;
                            DateTime dt2 = new DateTime(mm.Year, mm.Month, 1);
                            for (int j = 1; j <= DateTime.DaysInMonth(mm.Year, mm.Month); j++)
                            {
                                if (dt2.DayOfWeek != DayOfWeek.Saturday && dt2.DayOfWeek != DayOfWeek.Sunday)
                                {
                                    count3++;
                                }
                                dt2 = dt2.AddDays(1);
                            }
                            sb.AppendLine(string.Format("Month:" + mm.Month + "  " + "Days:" + count3));
    
                            mm = mm.AddMonths(1);
                        }
    
                        //
                        int count4 = 0;
                        DateTime dt3 = new DateTime(endDate.Year, endDate.Month, 1);
                        for (int i = dt3.Day; i <= endDate.Day; i++)
                        {
                            if (dt3.DayOfWeek != DayOfWeek.Saturday && dt3.DayOfWeek != DayOfWeek.Sunday)
                            {
                                count4++;
                            }
                            dt3 = dt3.AddDays(1);
                        }
                        sb.AppendLine(string.Format("Month:" + dt3.Month + "  " + "Days:" + count4));
                    }
                    else
                    {
                        Response.Write("<script> alert('please enter a former date');</script>");
                    }
                }
                else
                {
                    //Please refer to the above code.
                }            
            }

    If you have any other questions about my reply, please let me know freely.

    Best Regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 16, 2014 6:51 AM
  • User487807879 posted

    This will get you what you want, but you will see only number of working days between start and end date. So, if you enter 10/25/2014 as a start date, you will get 5 as a number of working days. If you want to get complete number of working days for the whole month, you will have to modify the code.

        protected void Page_Load(object sender, EventArgs e)
        {
            foreach (MonthDaysData lobj_Data in GetWorkingDays(new DateTime(2014, 5, 2), new DateTime(2014, 10, 18)))
            {
                Response.Write(lobj_Data.Month.ToString() + "/" + lobj_Data.Year.ToString() + ": " + lobj_Data.WorkingDays.ToString() + "<br/>");
            }
        }
    
        private List<MonthDaysData> GetWorkingDays(DateTime ldt_DateStart, DateTime ldt_DateEnd)
        {
            List<MonthDaysData> lobj_MonthDaysDataList = new List<MonthDaysData>();
            while (ldt_DateEnd.Date > ldt_DateStart.Date)
            {
                if (ldt_DateStart.DayOfWeek != DayOfWeek.Saturday && ldt_DateStart.DayOfWeek != DayOfWeek.Sunday)
                {
                    MonthDaysData lobj_MonthDaysData = lobj_MonthDaysDataList.FirstOrDefault(o => o.Month == ldt_DateStart.Month && o.Year == ldt_DateStart.Year);
                    if (lobj_MonthDaysData == null)
                    {
                        lobj_MonthDaysData = new MonthDaysData()
                        {
                            Year = ldt_DateStart.Year,
                            Month = ldt_DateStart.Month,
                            WorkingDays = 1
                        };
                        lobj_MonthDaysDataList.Add(lobj_MonthDaysData);
                    }
                    else
                    {
                        lobj_MonthDaysData.WorkingDays += 1;
                    }
                }
                ldt_DateStart = ldt_DateStart.AddDays(1);
            }
            return lobj_MonthDaysDataList;
        }
    
        public class MonthDaysData
        {
            public int Year { get; set; }
            public int Month { get; set; }
            public int WorkingDays { get; set; }
        }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 16, 2014 9:30 AM