Hello. Im trying to add 2 times in C# windows form app. I have a function where I am retrieving a time field from database and adding it to another time which is in a texbox. I am able to retrieve the time field but when I am adding and saving, it is saving the date also for example 6:00:00 + 4:00:00, i get Apr 14 2011 10:00PM. I have tried several ways with same results. I just need to add the 2 times.

DateTime Staff_Total_Hours = Convert.ToDateTime(cmd.ExecuteScalar().ToString());
Staff_Total_Hours = Staff_Total_Hours + TimeSpan.Parse(txtTime.Text);
DateTime Staff_Total_Hours = DateTime.Parse(textBox1.Text);
Staff_Total_Hours = Staff_Total_Hours + TimeSpan.Parse(txtTime.Text);
TextBox1.Text = "0";
DateTime zero = Convert.ToDateTime(TextBox1.Text);

DateTime dt1 = Convert.ToDateTime(cmd.ExecuteScalar().ToString());
DateTime dt1 = Convert.ToDateTime(total);
DateTime dt2 = Convert.ToDateTime(txtTime.Text);
TimeSpan dt3 = dt2.Subtract(zero);

 DateTime Staff_Total_Hours = dt1.Add(dt3);

None of them are giving the correct result. I will be veru grateful if anyone can help me!

ddanbe commented: Nice question! +9

Recommended Answers

All 13 Replies

Have you tried this?

DateTime.Now.AddHours(st)

You can use String.Format method, but it will return a string value of the time (not an actual time).

DateTime date = DateTime.Now;
string strTime = String.Format("{dd.MM.yyyy hh:mm}", date);

Do you explicitly wanna have a DateTime, and now a string?

If you only wanna add time (hours) you can simply use Add method in the time:

DateTime newTime = date.AddHours(2);
DateTime TestTime = DateTime.Parse(timeString1);
TestTime = TestTime + TimeSpan.Parse(timeString2);

i've tried all these but they don't work.

textBox1.Text  = cmd.ExecuteScalar().ToString();              
DateTime Staff_Total_Hours = textBox1.Text + TimeSpan.Parse(txtTime.Text);

what i am trying to do is to retrieve a value (which is saved as time for example 10:00:00) and add it to another value from a textbox.

Try this way

textBox1.text=cmd.ExecuteScalar().ToString();
DateTime Staff_Total_Hours = Convert.ToDateTime(textBox1.Text) +  Convert.ToDateTime(txtTime.Text);
DateTime Staff_Total_Hours = Convert.ToDateTime(textBox1.Text) +  Convert.ToDateTime(txtTime.Text);

Im getting this error at the above line: Operator '+' cannot be applied to operands of type 'System.DateTime' and 'System.DateTime'.

YOu cannot use + operator on DateTime. What exactly would you like to do?

Would you like to sum hours? I what format do you have hours?

In one textBox1 you have "10:00:00", that means "hour is 10am". What is in the 2nd textBox2?

DateTime Staff_Total_Hours = (Convert.ToDateTime(textBox1.Text)).ToTimeSpan().Add(  (Convert.ToDateTime(txtTime.Text)).ToTimeSpan());

try ths

I have read your 1st post, and this is the colution for your troubles: :)

DateTime hour1 = Convert.ToDateTime(textBox1.Text);
 DateTime hour2 = Convert.ToDateTime(textBox2.Text);
 hour1 = hour1.AddHours(hour2.Hour).AddMinutes(hour2.Minute).AddSeconds(hour2.Second);
 MessageBox.Show("Total date with hours is: " + hour1);

AS you can see, you have to convert both values to dateDate format. Then you have to add every sinlge date and/or time value to the date you want to "sum up". On this way you can add even years, months, miliseconds,..

bye, bye

DateTime Staff_Total_Hours = (Convert.ToDateTime(textBox1.Text)).ToTimeSpan().Add(  (Convert.ToDateTime(txtTime.Text)).ToTimeSpan());

does not solve the problem

Try this it will work IA :)

DateTime Time1=Convert.ToDateTime(txtTime.Text);
TimeSpan ts = new TimeSpan(Time1.Day, Time1.Hour, Time1.Minute, Time1.Second, Time1.Millisecond);
DateTime Staff_Total_Hours = DateTime.Parse(textBox1.Text).Add(ts);
DateTime Staff_Total_Hours = (Convert.ToDateTime(textBox1.Text)).ToTimeSpan().Add(  (Convert.ToDateTime(txtTime.Text)).ToTimeSpan());

does not solve the problem

Dont you see my post on this 2nd page on the top? This is your solution.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.