none
c# How get Days in month with previous and next days RRS feed

  • Question

  • How i can get the 35 days in month:

    • Starting week day in Monday;
    • Until the first day fill with last days of the previous month;
    • After the days of the current month fill with the first days of next month;

    I try with :

           var days = Enumerable.Range(1, DateTime.DaysInMonth(year, month)) 
                                 .Select(day => new CalendarItem(year, month, day))
                                 .ToList();


    But only get the current month


    Tuesday, July 24, 2018 9:06 AM

Answers

  • Your post title and your question seem different. Your question seems to be asking how to get the next and previous day . That can be done using the DateTime.AddDays method. 

    There are no months with 35 days so do you want all the days in a month? If so then keep incrementing the date until the month changes. I have wrapped this logic into a Date class in the library I use if you're interested. I've not bothered to add enumeration support to it or DateRange but that is an interesting prospect.

    class Program
    {
        static void Main ( string[] args )
        {
            var startDate = DateTime.Now;
            var numberOfDays = 35;
    
            var dates = startDate.GetRange(numberOfDays);
            foreach (var date in dates)
                Console.WriteLine(date);
        }
    }
    
    static class DateExtensions
    {
        public static IEnumerable<DateTime> GetRange ( this DateTime source, int days )
        {
            for (var current = 0; current < days; ++current)
            {
                yield return source.AddDays(current);
            };
        }
    }

    To find the first Monday of the month you'd start with the first of the month and enumerate until you get to the Monday. First Monday of the month may vary by definition though. Some people think it is the first actual Monday while others require that it is the first Monday in a week that is at least mostly in the Month.

    //In the DateExtensions class
    public static DateTime NextDayOfWeek ( this DateTime start, DayOfWeek dayOfWeek )
    {
        while (start.DayOfWeek != dayOfWeek)
            start = start.AddDays(1);
    
        return start;
    }
    
    //Example usage
    var firstMonday = new DateTime(2018, 6, 1).NextDayOfWeek(DayOfWeek.Monday);


    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, July 24, 2018 2:50 PM
    Moderator

All replies

  • Your post title and your question seem different. Your question seems to be asking how to get the next and previous day . That can be done using the DateTime.AddDays method. 

    There are no months with 35 days so do you want all the days in a month? If so then keep incrementing the date until the month changes. I have wrapped this logic into a Date class in the library I use if you're interested. I've not bothered to add enumeration support to it or DateRange but that is an interesting prospect.

    class Program
    {
        static void Main ( string[] args )
        {
            var startDate = DateTime.Now;
            var numberOfDays = 35;
    
            var dates = startDate.GetRange(numberOfDays);
            foreach (var date in dates)
                Console.WriteLine(date);
        }
    }
    
    static class DateExtensions
    {
        public static IEnumerable<DateTime> GetRange ( this DateTime source, int days )
        {
            for (var current = 0; current < days; ++current)
            {
                yield return source.AddDays(current);
            };
        }
    }

    To find the first Monday of the month you'd start with the first of the month and enumerate until you get to the Monday. First Monday of the month may vary by definition though. Some people think it is the first actual Monday while others require that it is the first Monday in a week that is at least mostly in the Month.

    //In the DateExtensions class
    public static DateTime NextDayOfWeek ( this DateTime start, DayOfWeek dayOfWeek )
    {
        while (start.DayOfWeek != dayOfWeek)
            start = start.AddDays(1);
    
        return start;
    }
    
    //Example usage
    var firstMonday = new DateTime(2018, 6, 1).NextDayOfWeek(DayOfWeek.Monday);


    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, July 24, 2018 2:50 PM
    Moderator
  • I tested and works, but there is a small bug

    In the line: start.AddDays(1);

    Is start.AddDays(-1);

    The final is:

    //In the DateExtensions class public static DateTime NextDayOfWeek ( this DateTime start, DayOfWeek dayOfWeek ) { while (start.DayOfWeek != dayOfWeek)

    start = start.AddDays(-1); return start; }

    Thursday, July 26, 2018 3:20 PM
  • If that works for you then good but it isn't going to behave correctly to me. Using -1 means it steps backwards. So it is no longer finding the next day of the week but the previous day of the week. If that is what you want then fine but I would recommend that you change the method name to match that it isn't going to find the next one.

    Michael Taylor http://www.michaeltaylorp3.net

    Thursday, July 26, 2018 3:25 PM
    Moderator