Nathan
Nathan

Reputation: 91

To get the mondays to saturdays in the current month

In a month i want to know mondays to sataurdays for the current month eg: in oct month 2011 there are

3-oct-2011 to 8-oct-2011,
10-OCt-11 to 15-Oct-11, 
17-Oct-11 to 22-oct-2011,
24-Oct-2011 to 29-Oct-2011 

all these sequence of days All these days like 3-oct-2011,4-oct-2011 ....29-oct-11 etc need to get in the array format or in the datatable.

Upvotes: 2

Views: 3785

Answers (3)

Baqer Naqvi
Baqer Naqvi

Reputation: 6504

Efficient solution;

var x = DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month);
int i = 1;
while (i <= x)
{
    if (new DateTime(DateTime.Now.Year, DateTime.Now.Month, i).DayOfWeek == DayOfWeek.Saturday)
    {
        Console.WriteLine(new DateTime(DateTime.Now.Year, DateTime.Now.Month, i));
        i += 6;
    }
    i++;
}

Upvotes: 0

suhail
suhail

Reputation: 1

most easy:

        int month = DateTime.Now.Month;
        int year = DateTime.Now.Year;
        int days= DateTime.DaysInMonth(year, month);
        int totalSaturdays = 0;
        for(int i=1;i<=days;i++)
        {
            var day = new DateTime(year, month, i);
            if(day.DayOfWeek==DayOfWeek.Saturday)
            {
                totalSaturdays++;
            }
        }
        Console.WriteLine(("Total Saturdays ="+totalSaturdays.ToString()));
        Console.ReadLine();

Upvotes: 0

Johannes Kommer
Johannes Kommer

Reputation: 6451

var today = DateTime.Today;
var daysInMonth = DateTime.DaysInMonth(today.Year, today.Month);

var dates = Enumerable.Range(1, daysInMonth)
                            .Select(n => new DateTime(today.Year, today.Month, n))
                            .Where(date => date.DayOfWeek != DayOfWeek.Sunday)
                            .ToArray();

This will look at the number of days in the current month, create a DateTime object for each, then only return those dates which are not a Sunday as an array.

var today = DateTime.Today;
var daysInMonth = DateTime.DaysInMonth(today.Year, today.Month);

var dates = Enumerable.Range(1, daysInMonth)
                            .Select(n => new DateTime(today.Year, today.Month, n))
                            .Where(date => date.DayOfWeek != DayOfWeek.Sunday)
                            .SkipWhile(date => date.DayOfWeek != DayOfWeek.Monday)
                            .TakeWhile(date => date.DayOfWeek != DayOfWeek.Monday || (date.DayOfWeek == DayOfWeek.Monday && daysInMonth - date.Day > 7))
                            .ToArray();

This will do the same, except get rid of any Monday -> Saturday ranges which are not in the current month. (Week started in the previous month, or ends in the next).

Edit:

Here is a .NET 2 solution which will do the same thing as my previously posted LINQ solution.

        DateTime today = DateTime.Today;
        int daysInMonth = DateTime.DaysInMonth(today.Year, today.Month);

        List<DateTime> dates = new List<DateTime>();

        bool foundFirst = false;

        for (int n = 1; n <= daysInMonth; n++)
        {
            var date = new DateTime(today.Year, today.Month, n);

            // Skip untill we find the first Monday of the month.
            if (date.DayOfWeek != DayOfWeek.Monday && !foundFirst)
                continue;

            foundFirst = true;

            // Add all days except Sundays. 
            if (date.DayOfWeek != DayOfWeek.Sunday)
                dates.Add(date);

            int remainingDays = daysInMonth - n;

            // Verify that there are enough days left in this month to add all days upto the next Saturday.
            if (date.DayOfWeek == DayOfWeek.Saturday && remainingDays < 7)
                break;
        }

        DateTime[] dateArray = dates.ToArray();

Upvotes: 8

Related Questions