0
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);
        }
2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by jellybeannn
0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.