简体   繁体   中英

How do I add TimeSpan in C# inside Loop?

Can anyone please tell me I need to add time with interval with in the for loop. This is my C# code.

var t1 = selectedTime.TimeOfDay;
t1 = 8.00 //assum time is 8.00           
for (int i = 0; i <=5; i++)  {
    var duration = TimeSpan.FromMinutes(15);
    var t3 = duration;
}

My output should be:

8.00
8.15
8.30
8.45
9.00

The gotcha in your original code is increment and persist the new value; in this snippet I updated the t1 variable:

var selectedTime = DateTime.Today.AddHours(8);
var t1 = selectedTime.TimeOfDay;    //assume time is 8.00           
for (int i = 0; i <=5; i++)
{
    Console.WriteLine(t1);
    t1 += TimeSpan.FromMinutes(15);
}

Try like this:

var t1 = DateTime.Now.TimeOfDay;
    //t1 = 8.00 //assum time is 8.00   
    var duration = TimeSpan.FromMinutes(15); 
    Console.WriteLine(t1); // print current time
    for (int i = 0; i <=4; i++)
    {        
        t1 += duration; // add timespan 4 times
        Console.WriteLine(t1);  
    }

You can add TimeSpan to the DateTime easily. You just need to make sure you are remembering the previous value or by increasing the time span (eg. 15 min, 30 min etc) in each loop cycle.

TimeSpan is added using the + operator with another TimeSpan , for example, I can implement this 15 seconds interval loop using Linq as the following:

TimeSpan initialTimeSpan = new TimeSpan(0, 4, 2);
IEnumerable<TimeSpan> spans = Enumerable.Range(0, 5)
    .Select(x => x * 15)
    .Select(x => initialTimeSpan + TimeSpan.FromSeconds(x));

foreach (TimeSpan span in spans)
{
    Console.WriteLine(span);
}

To add it to a DateTime use the Add method:

DateTime date = DateTime.Now;
TimeSpan initialTimeSpan = new TimeSpan(0, 4, 2);
date = date.Add(initialTimeSpan);

Or you can just add the period you wish without using TimeSpan by using the AddSeconds / AddMinutes / AddHours / AddMilliseconds / AddTicks / AddYears / AddDays / AddMonths methods:

DateTime date = DateTime.Now;
TimeSpan initialTimeSpan = new TimeSpan(0, 4, 2);
date = date.AddSeconds(5);

You can use everything in a loop like that:

DateTime initialTime = DateTime.Now;
for (DateTime loopTime = initialTime; loopTime < initialTime.AddMinutes(2); loopTime = loopTime.AddSeconds(15))
{
    Console.WriteLine(loopTime);
}

And you will get the following output:

26/09/2015 13:00:33
26/09/2015 13:00:48
26/09/2015 13:01:03
26/09/2015 13:01:18
26/09/2015 13:01:33
26/09/2015 13:01:48
26/09/2015 13:02:03
26/09/2015 13:02:18

To get the output in the format you wanted you can change the printing to the following:

Console.Write(loopTime.Minute + ".");
if (loopTime.Second < 10)
{
    Console.Write(0);
}
Console.WriteLine(loopTime.Second);

And you will get this output for the same hours shown above:

0.33
0.48
1.03
1.18
1.33
1.48
2.03
2.18

Alternatively, you could use the DateTime.AddMinutes() method:

DateTime t1 = DateTime.Now.AddHours(8);

for(int i = 0; i < 5; i++) {
    Console.WriteLine(t1.AddMinutes(15 * i).ToString("HH:mm")); 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM