c# - how to work with Datetime to create month and year -
i want to create somthing this:
may, 2013
april, 2013
march, 2013
february, 2013
january, 2013
december, 2012
november, 2012
what have written this:
var currentdate = datetime.now; var list = new list<archiveviewmodel>(); (var startdate = new datetime(2013, 1, 1); startdate.month <= currentdate.month; startdate = startdate.addmonths(1)) { list.add(new archiveviewmodel { month = startdate.month, year = startdate.year, formatteddate = startdate.tostring("mmmm, yyyy") }); }
and archiveviewmodel this:
public class archiveviewmodel { public int month { get; set; } public int year { get; set; } public string formatteddate { get; set; } }
however, creates months of specific year (2012 or 2013):
january, 2013 february, 2013 march, 2013 april, 2013 may, 2013
and if change startdate (2012,1,1) create this:
january, 2012 february, 2012 march, 2012 april, 2012 may, 2012
but want this:
november,2012 december , 2012 january, 2013 february, 2013 march, 2013 april, 2013 may, 2013
simply change loop condition from
startdate.month <= currentdate.month
to
startdate <= currentdate
Comments
Post a Comment