Click here to Skip to main content
14,838,821 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

I have a Drop down List box, contains 'Last 7 days', 'Last 15 days' and 'Last 30 days'.

From this I need to get dates between DateTime.Now and DateTime.Now - 7 days

Please help me how to achieve this.

Thanks in advance

Use below code to populate your dropdown

DataTable dt_dates = new DataTable();

for (int i = 0; i < 7; i++)
    DataRow dr_dates = dt_dates.NewRow();
    dr_dates["Date"] = DateTime.Now.AddDays(-i).ToShortDateString();

Combobox.Datasource = dt_dates;
get startdate and enddate

DateTime StartingDate= DateTime.Now.AddDays(-30);

then get list of dates between these dates using below method

private List<datetime> GetDateRange(DateTime StartingDate, DateTime EndingDate)
if (StartingDate > EndingDate)
return null;
List<datetime> rv = new List<datetime>();
DateTime tmpDate = StartingDate;
tmpDate = tmpDate.AddDays(1);
} while (tmpDate <= EndingDate);
return rv;
int count = 7;
var dates = Enumerable.Range(1-count, count).Select(i => DateTime.Today.AddDays(i));
Gabriel Szabo 17-Oct-13 4:15am
And remember not to run this code on midnight... ;)
Use Substring to extract date/month and year

string fr = DateTime.Today.ToShortDateString();
string from = fr.Substring(6, 4) + "-" + fr.Substring(3, 2)
 + "-" + fr.Substring(0, 2);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900