Get the next first day of the month

benjoe

New member
Joined
Apr 12, 2019
Messages
3
Programming Experience
1-3
I have a code that gives me the Nth day of the month. between two dates. Now what I want is to check if the Start date is not the Nth day of the month, move the start date to the following month same first day. For example.
a user entered Start date of 06/27/2019 which is a Thursday but want the first day of the month for Monday which was 06/3/2019, which is already passed so the new start date should be 07/1/2019. How to I get my code to achieve this.
Below is my complete code.
C#:
class Program
    {
        public class BusinessWeekDays
        {
            public DateTime Monday;
            public DateTime Sunday;
        }

        public static List<DateTime> GetDatesByOrdinalDayV2(DateTime startDate, DateTime endDate, DayOfWeek dayOfWeek,
            int ordinal)
        {
            var foundDates = new List<DateTime>();
            ordinal = Math.Min(Math.Max(ordinal, 1), 5);
            while (startDate < endDate)
            {
                var month = startDate.Month;

                var workingDate = new DateTime(startDate.Year, month, 1);

                workingDate = (int)workingDate.DayOfWeek > (int)dayOfWeek
                    ? workingDate.AddDays(7 - (int)workingDate.DayOfWeek + (int)dayOfWeek)
                    : workingDate.AddDays((int)dayOfWeek - (int)workingDate.DayOfWeek);

                workingDate = workingDate.AddDays((ordinal - 1) * 7);
                workingDate = workingDate.Month != month ? workingDate.AddDays(-7) : workingDate;
                foundDates.Add(workingDate);
                startDate = startDate.AddMonths(1);
            }

            return foundDates;
        }

        static void Main(string[] args)
        {

            var StartDate = DateTime.Parse("06/27/2019");
            var SeriesEndDate = DateTime.Parse("09/30/2019");
            var months = new List<BusinessWeekDays>();
            var secondTuesday = GetDatesByOrdinalDayV2(StartDate, SeriesEndDate, DayOfWeek.Monday, 1);
            var dateStrings = secondTuesday
                .Select(dateTime => dateTime.ToString("d MMM yyyy")).ToList();

            foreach (var date in dateStrings)
            {
                if (StartDate.DayOfWeek != DayOfWeek.Monday)----> start date is not Monday
                {
                    months.Add(new BusinessWeekDays
                    {
                        Monday = Convert.ToDateTime(date).AddMonths(1),----Start Monday 7/1/2019
                        Sunday = Convert.ToDateTime(date).AddMonths(1).AddDays(7)
                    });
                    Console.WriteLine(months);
                }
                else
                {
                    months.Add(new BusinessWeekDays
                    {
                        Monday = Convert.ToDateTime(date),
                        Sunday = Convert.ToDateTime(date).AddDays(7)
                    });
                }
            }

         

        }
 
Are you asking how to find first monday of a month?
C#:
private DateTime FirstMonday(int year, int month) {
    return Enumerable.Range(1, 7)
                     .Select(day => new DateTime(year, month, day))
                     .Where(date => date.DayOfWeek == DayOfWeek.Monday)
                     .First();
}
 
Hi John
I don't think you read my question very well. My code already gives me the first, Monday, Tuesday, etc. All that I am asking is, the first Monday for June is 3rd-->6/3/2019. So a user entered 6/27/2019 which is passed the 6/3/2019, how do I get the new start date to begin on the 7/1/2019
 
So the question is: if current date is not first monday of the month, then get first monday of next month?
 
Last edited:
That is not difficult to do with the method I posted, you call it once to get first monday of given date (its Year and Month values), if given date is less or equal to that value you have upcoming monday at hand.
If not you call it again to get first monday of the next month. You can use DateTime.AddMonths method to get a date value of next month so that you have correct Year and Month to pass to the method.
 
Last edited:
Back
Top Bottom