views:

1493

answers:

6

How do I add times in C#? For example:

Time = "14:20 pm" +  "00:30 pm"
+16  A: 

I have insufficient reputation to add a comment - What do you expect the answer to that to be? 00:30pm isn't even a valid time - it would be 00:30am or 12:30pm

Joel Mansford
+1 so you can speak more sense, faster :)
leppie
Doesn't make sense to me that the post that was the least help got the most votes....the fact they put in pm was probably a typo...
James
@James: actually, it makes perfect sense - Joel gave an honest and precise answer to the question asked, while everyone else just guessed at what he meant... While it's quite possible that apeksha is just careless and lazy, there's also a chance that this format was a specific requirement given him by someone else, in which case none of the other answers will do him a lick of good.
Shog9
@Apeksha also didn't state what his desired output was. My thought was that maybe he hadn't thought this through.I do agree though, I haven't provided a solution - I had hoped to gain further input to give a specific, accurate solution. As @Apeksha hasn't come back how can any of us do that?
Joel Mansford
@Shog9 and @Joel, if you look at the question it specifically states "How do I add times in C#?"...then provides an example, therefore, the question was quite clear. The example was just to illustrate a sample of what he was looking for...therefore, I still do not see how this post gained the most votes...it doesn't make sense as I said before.
James
@James: and if you read this answer, or the comments on the question itself, you'll see that the example made no sense. Again, if this was the requirement that apeksha was given, he needs to go back to those who provided it and ask for clarification. Given lack of further details from him, this may well be what he has done...
Shog9
@Shog9 don't get me wrong I understand his example contained an invalid time. However, at the end of the day the question was clear, the example was irrelevant in my eyes. What I took from it was How do I add 2 times in C# that will be in the format of "HH:MM AM/PM" string.
James
@James - this is going to sound pedantic but adding "HH:MM AM/PM" to "HH:MM AM/PM" is actually adding two dates together which doesn't make sense.Adding a Timespan to a date/time is the only thing that does make sense to do given one parameter in this format. The only other thing that's sensible is adding 0hrs 30mins on to 14hrs 20mins. This is different again - see the ambiguity?
Joel Mansford
@Joel I don't see how the AM/PM to the time string makes it a date? Surely you would need to specify the full "DD/MM/YYYY HH:MM AM" to make it a date. Anyway, think this conversation has dragged on a little too much...I believe that apeksha asked a clear and answerable question which I, as did many answered. What I am saying is it is more than likely a typo in the example hence the real question was simply how do you add 2 times together in C#....just found it odd that this post recieved the most votes as it did not answer the question in anyway.
James
If it was indeed a typo, then no harm done - he got plenty of answers made with that assumption. If not, then he got at least one answer indicating a need to define how input data will be interpreted. Unless he comes back and accepts an answer, we'll never know for sure which one was actually the case...
Shog9
+3  A: 

You would want to convert both times into a TimeSpan objects.

This will give you explicit access to the Hours/Minutes values of each time and you can add them together.

See TimeSpan from MSDN.

James
Nope, just the second one. DateTime + TimeSpan = DateTime
leppie
Yeah your right Leppie, I was just being more thorough so they could access the hours/minutes property for both times :)
James
+14  A: 

Assuming you want to add 30 minutes to a given DateTime, you can use AddMinutes.

TestTime.AddMinutes(30);

Another way of doing it:

DateTime TestTime = DateTime.Parse("22 Jun 2009 14:20:00");
// Add 30 minutes
TestTime = TestTime + TimeSpan.Parse("00:30:00");
Colin Pickard
+2  A: 
 TimeSpan t1 = new TimeSpan(14, 20,0);
 TimeSpan t2 = new TimeSpan(0,30,0);
 Console.Out.WriteLine(t1 + t2);
Kirschstein
+1  A: 

You can't add those, just like you can't add "14:20PM" and the color red. You can add a time and a timespan (14:20PM + 30 minutes) or two timespans (2 hours+30 minutes). But you cannot add two times.

To make this even clearer, consider what would happen if you could add two times: 14.20 + 00:30 (EST) = 23.20 + 09:30 (UTC)

MSalters
+1  A: 

Try this (although 0:30pm doesn't make sense):

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(new StringTime("14:20 pm").Add(new StringTime("0:30 pm")));
        Console.WriteLine(new StringTime("15:00 pm").Add(new StringTime("0:30 pm")));
        Console.WriteLine(new StringTime("5:00am").Add(new StringTime("12:00pm")));
    }
}

class StringTime
{
    public int Hours { get; set; }
    public int Minutes { get; set; }
    public bool IsAfternoon { get; set; }

    public StringTime(string timeString)
    {
        IsAfternoon = timeString.Contains("pm");
        timeString = timeString.Replace("pm", "").Replace("am", "").Trim();

        Hours = int.Parse(timeString.Split(':')[0]);
        Minutes = int.Parse(timeString.Split(':')[1]);
    }

    public TimeSpan ToTimeSpan()
    {
        if (IsAfternoon)
        {
            if (Hours < 12)
            {
                Hours += 12;
            }
        }
        return new TimeSpan(Hours, Minutes, 00);
    }

    public TimeSpan Add(StringTime time2)
    {
        return this.ToTimeSpan().Add(time2.ToTimeSpan());
    }
}

Output (the value before the dot are days):

1.02:50:00
1.03:30:00
17:00:00