Display all months from (today) and go back 14 months

TheCoder 91 Reputation points
2022-06-02T13:33:55.99+00:00

I have a requirement to return a rolling 14 months to the app. I'm creating an WebAPI

So, I need to go back 14 months from todays date.

I have this so far, but drawing a blank:

[HttpGet("[months]") //allows them to define the months to go back to
public ActionResults ReportingMonths(int months)
{
    var fromDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddMonths(months);



}

how can I loop through the from date and get all of the months that go back the number of months passed into the call?

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,188 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,277 questions
{count} votes

Accepted answer
  1. AgaveJoe 26,136 Reputation points
    2022-06-02T14:52:09.933+00:00

    do you need the Web API programming pattern too?

    [Route("api/[controller]")]  
    [ApiController]  
    public class ReportsController : ControllerBase  
    {  
      
        [HttpGet("{months}")]   
        public ActionResult ReportingMonths(int months = 14)  
        {  
            List<DateTime> dates = new List<DateTime>();  
            DateTime firstOfTheCurrentMonth = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);  
      
            for (int i = 0; i < months; i++)  
            {  
                dates.Add(firstOfTheCurrentMonth.AddMonths(i * -1));  
            }  
      
            return Ok(dates);  
        }  
    }  
    

    If the {months} route parameter is missing then the default is 14; int months = 14.

    https://localhost:7208/api/Reports  
    

    If the {months} route parameter exits then the route parameter is used.

    https://localhost:7208/api/Reports/2  
    

    It might be a good idea to go through a Web API getting started tutorial and/or read the route reference documentation.

    Tutorial: Create a web API with ASP.NET Core
    Routing to controller actions in ASP.NET Core

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. AgaveJoe 26,136 Reputation points
    2022-06-02T14:21:52.963+00:00

    You want a list of dates going back a variable number of months?

    static void Main(string[] args)
    {
        int months = 14;
        List<DateTime> dates = new List<DateTime>();
        DateTime firstOfTheCurrentMonth = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
    
        for (int i = 0; i < months; i++)
        {
            dates.Add(firstOfTheCurrentMonth.AddMonths(i * -1));
        }
    
        foreach(DateTime date in dates)
        {
            Console.WriteLine(date.ToShortDateString());
        }
    
    }
    

    Results

    6/1/2022
    5/1/2022
    4/1/2022
    3/1/2022
    2/1/2022
    1/1/2022
    12/1/2021
    11/1/2021
    10/1/2021
    9/1/2021
    8/1/2021
    7/1/2021
    6/1/2021
    5/1/2021
    

    If you do not want to include this month then...

    for (int i = 1; i <= months; i++)