Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 486
ALEXALEXIYEV Avatar asked Feb 04 '09 10:02

ALEXALEXIYEV


People also ask

How to Add Time in c#?

The DateTime. AddMinutes() method in C# is used to add the specified number of minutes to the value of this instance. It returns the new DateTime.

How to Add 2 TimeS in c#?

Adding two datetimes from strings: var result = DateTime. Parse(firstDate) + DateTime. Parse(secondDate);


2 Answers

myDateTimeVariable.Add(new TimeSpan(2,2,2));
like image 90
cbp Avatar answered Sep 26 '22 08:09

cbp


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
like image 22
alex2k8 Avatar answered Sep 23 '22 08:09

alex2k8