Reputation: 1237
I am trying to bind the values to the dropdown. I need to bind previous 12 month-year format as below
This is how i want to bind the data.
Binding Text: March 2015 February 2015 January 2015 December 2014 November 2014 October 2014 September 2014 August 2014 July 2014 June 2014 May 2014 April 2014
Binding Value: 3 2015 2 2015 1 2015 12 2014 11 2014 10 2014 9 2014 8 2014 7 2014 6 2014 5 2014 4 2014
Appreciate your time. Thanks
Upvotes: 0
Views: 1603
Reputation: 2372
Simply
for (int i = -11; i <= 0; i++)
{
var d = DateTime.Now.AddMonths(i);
var dStartingFromDayOne = new DateTime(d.Year, d.Month, 1);
var ds = dStartingFromDayOne.ToString("MMMM yyyy");
Console.WriteLine(ds);
}
Using linq
var last12Months = Enumerable.Range(-11, 12).Select(i => DateTime.Now.AddMonths(i)).Select(d => new DateTime(d.Year,d.Month,1).ToString("MMMM yyyy"));
Upvotes: 1