Add or Sum of hours like 13:30+00:00:20=13:30:20 but how?


Question

I want to add seconds (00:00:02) or minutes (00:00:20) on datetime value (may be stored string type) but how? Examples:

13:30+02:02:02= 15:32:02 ,
13:30+00:00:01= 13:30:01 ,
13:30+00:01:00=13:31:00 or 13:30 (not important) 

Can you help me? I need your cool algorithm :) Thanks again...

1
6
9/28/2012 8:10:28 AM

Accepted Answer

If you choose to use the TimeSpan, be aware about the Days part:

TimeSpan t1 = TimeSpan.Parse("23:30");
TimeSpan t2 = TimeSpan.Parse("00:40:00");
TimeSpan t3 = t1.Add(t2);
Console.WriteLine(t3); // 1.00:10:00

With DateTime:

DateTime d1 = DateTime.Parse("23:30");
DateTime d2 = DateTime.Parse("00:40:00");
DateTime d3 = d1.Add(d2.TimeOfDay); 
Console.WriteLine(d3.TimeOfDay); // 00:10:00
4
2/4/2009 11:39:29 AM

myDateTimeVariable.Add(new TimeSpan(2,2,2));

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon