locked
Get Working days for current month RRS feed

  • Question

  • User-1901014284 posted

    Hi,

    I have the below code which pulls the amount of days for the current month which runs with no problem and returns the correct value (30 for September), I am looking to get the amount of working days for the current month (to not include Saturdays and Sundays).

    c# Code below:

    int noofdays = DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month);
    WorkingDaysPerMonthLabelMonthly.Text = "This Month Days: " + noofdays.ToString();

    Is there a way to tweak my code above to get only working days to be counted?

    Many thanks 

    Jonny

    Thursday, September 27, 2018 9:22 AM

Answers

  • User475983607 posted

    Thank you very much mgebhard but is there a way to get this to load on the Page_Load event?

    Just call the method.

            protected void Page_Load(object sender, EventArgs e)
            {
                int weekDays = WeekDaysInMonth(DateTime.Now.Year, DateTime.Now.Month);
                Label1.Text = weekDays.ToString();
            }
    
            protected int WeekDaysInMonth(int year, int month)
            {
                int days = DateTime.DaysInMonth(year, month);
                List<DateTime> dates = new List<DateTime>();
                for (int i = 1; i <= days; i++)
                {
                    dates.Add(new DateTime(year, month, i));
                }
    
                int weekDays = dates.Where(d => d.DayOfWeek > DayOfWeek.Sunday & d.DayOfWeek < DayOfWeek.Saturday).Count();
                return weekDays;
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 28, 2018 11:02 AM

All replies

  • User475983607 posted

    Try...

    public static int WeekDaysInMonth(int year, int month)
    {
        int days = DateTime.DaysInMonth(year, month);
        List<DateTime> dates = new List<DateTime>();
        for(int i = 1; i <= days; i++)
        {
            dates.Add(new DateTime(year, month, i));
        }
    
        int weekDays = dates.Where(d => d.DayOfWeek > DayOfWeek.Sunday & d.DayOfWeek < DayOfWeek.Saturday).Count();
        return weekDays;
    }

    Thursday, September 27, 2018 8:14 PM
  • User-1901014284 posted

    Thank you very much mgebhard but is there a way to get this to load on the Page_Load event?

    Friday, September 28, 2018 8:55 AM
  • User475983607 posted

    Thank you very much mgebhard but is there a way to get this to load on the Page_Load event?

    Just call the method.

            protected void Page_Load(object sender, EventArgs e)
            {
                int weekDays = WeekDaysInMonth(DateTime.Now.Year, DateTime.Now.Month);
                Label1.Text = weekDays.ToString();
            }
    
            protected int WeekDaysInMonth(int year, int month)
            {
                int days = DateTime.DaysInMonth(year, month);
                List<DateTime> dates = new List<DateTime>();
                for (int i = 1; i <= days; i++)
                {
                    dates.Add(new DateTime(year, month, i));
                }
    
                int weekDays = dates.Where(d => d.DayOfWeek > DayOfWeek.Sunday & d.DayOfWeek < DayOfWeek.Saturday).Count();
                return weekDays;
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 28, 2018 11:02 AM
  • User-1901014284 posted

    Thank you very much, this works perfectly :)

    Monday, October 1, 2018 8:57 AM