Click here to Skip to main content
15,938,218 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...
[no name] 18-Mar-13 9:15am    
What have you tried? Did you try AddDays(5)?

Suppose you could do it like this too.

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.

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)
Share this answer
CHill60 18-Mar-13 9:37am    
I'm not convinced that this answers the OPs problem?

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,

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,

Share this answer

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