Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to insert date with timespan in for loop one after the other in c#...For instance if its current date and the timespan is 5 days first data must enter 5 days greater then second data 10 days later and so on...
Posted
Comments
[no name] 18-Mar-13 9:15am    
What have you tried? Did you try AddDays(5)?

Hi,

What i understand from your query is, if you have 3 records and you would like to insert then you will enter 18+5 (ie todays date and 5), 18+10, 18+15.

To do so you need to use for loop like,

C#
for(int  i = 5,j=0; j< 3; j++, i = i+5)
{
// i should be added each time in your current date.
} 

//Note : above code is just to give you idea about logic, syntax may be differ a little bit.

Hope this will help you,

Thanks
-Amit
 
Share this answer
 
Hi,
Suppose you could do it like this too.

C#
public void Date()
{
    int dateSpan= 5;
    int numberOfIteration = 3;
    for (int i = 1; i <= numberOfIteration; ++i)
    {
        DateTime date = DateTime.Now.AddDays(i * dateSpan);
    }
}


dateSpan is the number of days you want to increment by.
numberOfIteration is the number of times you wanted increment the date.

So the date will have added days of dateSpan multiplied by count.

so the first date will be current day + (1 x 5);
so the second ate will be current day + (2 x 5);

and so on....


I hope this helps.

Regards
Jegan
 
Share this answer
 
Use comparison like given below to add values in ascending or descending order

DateTime dt = DateTime.Now;
DateTime dt2 = new DateTime(2013,03,18,18,49,00);
if (dt > dt2)
{
DropDownList1.Items.Add(dt2.ToString());
}
 
Share this answer
 
v2
Comments
CHill60 18-Mar-13 9:37am    
I'm not convinced that this answers the OPs problem?

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