How do you calculate dates from the database by adding 3 months?
I created a LINQ query that only selects the column EndYearMonth from the database. I was trying to set up a variable in the parameters called EndYearMonth which is also placed in the DateTime object. I got the DateTime function code using this link https://www.tutorialspoint.com/datetime-addmonths-method-in-chash. The problem is that this tutorial doesn't show you how to add three months from the database because there is only one date that is hardcoded.
Here is my code below
[HttpGet]
public ActionResult GetFinance(int EndYearMonth )
{
var dates = _context.Finances
.Select(e => e.EndYearMonth
)
.ToList();
DateTime d1 = new DateTime(EndYearMonth);
DateTime Q1 = d1.AddMonths(3);
DateTime Q2 = d1.AddMonths(6);
DateTime Q3 = d1.AddMonths(9);
DateTime[] array = {Q1, Q2 , Q3};
return Ok(array);
}
When I click send to get the request the output is this
The expected outcome should be this
This is the original data coming from the database
Use this code :
[HttpGet]
public ActionResult GetFinance()
{
var dates = _context.Finances
.Select(e => e.EndYearMonth)
.ToList();
int arrayLength = dates.Count() * 4;
DateTime[] array = new DateTime[arrayLength];
int index = 0;
foreach (var strDate in dates)
{
DateTime d1 = DateTime.Parse(strDate);
DateTime Q1 = d1.AddMonths(3);
DateTime Q2 = d1.AddMonths(6);
DateTime Q3 = d1.AddMonths(9);
array[index] = d1;
array[index + 1] = Q1;
array[index + 2] = Q2;
array[index + 3] = Q3;
index += 4;
}
return Ok(array);
}