views:

438

answers:

8

Hello coders!!!

i want to add second(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) how can you help me? I need your cool algoritm :) thanks again...

+1  A: 

Not really sure what you're after, but can you not just use the built in functions to C#'s DateTime object?

DateTime myDate = DateTime.Now;

myDate = myDate.AddHours(1);
myDate = myDate.AddMinutes(30);
myDate = myDate.AddSeconds(45);
Robin Day
+7  A: 
myDateTimeVariable.Add(new TimeSpan(2,2,2));
cbp
+1  A: 

use the TimeSpan structure. you can add TimeSpans together, or you can add a TimeSpan to a DateTime to produce a new DateTime.

cruizer
ok but 13:30:32= 11:30:20 how can add two datetime?
Phsika
A: 

ok Look please my question is not simple. i want to sum two datetime? 12:30:22+ 11:20:22 = ?

but how ?

Phsika
Edit your question if you don't think it's being answered.
drhorrible
+3  A: 

Adding two datetimes from strings:

var result = DateTime.Parse(firstDate) + DateTime.Parse(secondDate);

Adding a string time to a datetime:

var result = existingDateTime.Add(TimeSpan.Parse(stringTime);

Adding time as in your example:

var result = TimeSpan.Parse("12:30:22") + TimeSpan.Parse("11:20:22");

Finally, your example as dates (not tested!):

var result = DateTime.Parse("12:30:22") + DateTime.Parse("11:20:22");

Note that this is sloppy coding, but you get the idea. You need to verify somehow that the string is actually parseable.

Thomas Eyde
i don't have vs 2008. i need vs 2005. thanks!
Phsika
Solution: TimeSpan a = TimeSpan.Parse("12:30:22") + TimeSpan.Parse("11:20:22"); Console.Write(a.ToString()); Console.ReadKey();Thanks everyBody! i love Stackoverflow...
Phsika
Then mark this answer as accepted.
drhorrible
+1  A: 

You should have a look at TimeSpan.Parse. This converts a string to a TimeSpan object. That way you can do stuff like

TimeSpan a = TimeSpan.Parse(timeStringA)+TimeSpan.Parse(TimeStringB);

To split a string like "00:00:20+00:01:00" look at string.split

stringA = timeSting.split('+')[0];
stringb = timeSting.split('+')[1];
Sorskoot
A: 

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
alex2k8
+2  A: 

The problem is more abstract. As already mentioned, in .NET there are two types - DateTime and TimeSpan. The DateTime type represents a specific point in time. It's not an interval of time. It's a specific location in all time since the birth of the Universe. Even if you set the year/month/day components to 0, it will still represent some absolute point in time. Not a length of time.

The TimeSpan on the other hand represents some interval. 1 minute, 2 days, whatever. It's not specified WHEN, just HOW LONG.

So if you were to subtract two DateTime objects you would get a TimeSpan object that specifies how much time there is between them. And if you add a TimeSpan to a DateTime you get another DateTime. But you can't add a DateTime to another DateTime - that would make no sense.

It sounds to me like you should be working with TimeSpans all the time, because you are dealing with lengths of time, not absolute points in time. If you get these lengths from your source as a DateTime then that's actually not correct, and you should convert them to TimeSpans somehow. The parsing method is one way that has been suggested, but you might also try to subtract zero DateTime from it. That might be faster and more culture-independant.

Vilx-