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

20,810

Solution 1

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

Solution 2

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

Solution 3

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.

Solution 4

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

Solution 5

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.

Share:
20,810
Penguen
Author by

Penguen

Updated on August 17, 2020

Comments

  • Penguen
    Penguen over 3 years

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

  • Penguen
    Penguen about 15 years
    Solution: TimeSpan a = TimeSpan.Parse("12:30:22") + TimeSpan.Parse("11:20:22"); Console.Write(a.ToString()); Console.ReadKey(); Thanks everyBody! i love Stackoverflow...
  • Gert Arnold
    Gert Arnold over 11 years
    At StackOverflow please don't just paste code, but also explain your approach. In this specific case you may also want to explain what your late answer adds to the answers already given (and accepted).