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:
Hi,

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
Posted

Use below code to populate your dropdown

DataTable dt_dates = new DataTable();
dt_dates.Columns.Add("Date");

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

Combobox.Datasource = dt_dates;
Combobox.databind();
   
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;
do
{
rv.Add(tmpDate);
tmpDate = tmpDate.AddDays(1);
} while (tmpDate <= EndingDate);
return rv;
}
   
C#
int count = 7;
var dates = Enumerable.Range(1-count, count).Select(i => DateTime.Today.AddDays(i));
   
Comments
Gabriel Szabo 17-Oct-13 4:15am
   
And remember not to run this code on midnight... ;)
Use Substring to extract date/month and year

C#
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