public List<DateTime> GetDates(DateTime startDate, DateTime endDate, int valuationDay)
    {
        TimeSpan span;
        span = endDate - startDate;

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

        DateTime dateToParse = DateTime.MinValue + span;

        int yearMonths = 0;
        if (dateToParse.Year > 0)
        {
            yearMonths = (dateToParse.Year - 1) * 12;
        }

        int monthCount = (yearMonths + dateToParse.Month) - 1;

        for (int i = 0; i <monthCount; i++)
        {
            DateTime currDate = endDate.AddMonths(-(monthCount - i));
            int lastDayOfMonth = DateTime.DaysInMonth(currDate.Year, currDate.Month);
            currDate = new DateTime(currDate.Year, currDate.Month, lastDayOfMonth);
            if (currDate.Day <= valuationDay)
            {
                result.Add(currDate);
            }
            else
            {
                currDate = new DateTime(currDate.Year, currDate.Month, valuationDay);
                result.Add(currDate);
            }
        }

        return result;
    }

this function creates dates from start to end each month on a valuationDay(eg 10th of each month) but if start.Day is bigger than the valuationDay, it must go over to the next month.

I've tried this in the function, but it doesn't do anything

if (startDate.Day > vvaluationDay - 15)
        {
            startDate.AddMonths(1);
        }

Recommended Answers

All 2 Replies

I've tried this in the function, but it doesn't do anything

if (startDate.Day > vvaluationDay - 15)
        {
            startDate.AddMonths(1);
        }

Instead try this:

if (startDate.Day > vvaluationDay - 15)
        {
            startDate = startDate.AddMonths(1);
        }

startDate.AddMonths returns a new DateTime it does not modify itself.

Hope this helps

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.