如何在c#中一个接一个地在for循环中插入带有时间跨度的日期 [英] How to insert date with timespan in for loop one after the other in c#

查看:94
本文介绍了如何在c#中一个接一个地在for循环中插入带有时间跨度的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在c#中依次在for循环中插入带有时间跨度的日期...例如,如果当前日期和时间跨度为5天,则首先数据必须输入5天,然后输入10天后的第二个数据,依此类推。 ..

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...

推荐答案





如果您有3条记录,我的理解是什么如果您要插入,则输入18 + 5(即今天的日期和5),18 + 10,18 + 15.



这样做你需要用于循环,例如,


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,

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.



希望这会对你有所帮助,



谢谢

-Amit


Hope this will help you,

Thanks
-Amit




假设你也可以这样做。



Hi,
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是你想要增加的天数。

numberOfIteration是你想要增加日期的次数。



所以这个日期会增加dateSpan乘以count的天数。



因此第一个日期将是当天+(1 x 5);

因此第二次吃饭将是当天+(2 x 5);



等......





我希望这会有所帮助。



问候

Jegan



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


使用如下给出的比较按升序或降序添加值



DateTime dt = DateTime.Now;

DateTime dt2 = new DateTime(2013,03,18,18,49,00);

if(dt> dt2)

{

DropDownList1.Items.Add(dt2.ToString());

}
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());
}


这篇关于如何在c#中一个接一个地在for循环中插入带有时间跨度的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆