954,510 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Adding time in C#

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!

kirtee2209
Light Poster
28 posts since Apr 2011
Reputation Points: 19
Solved Threads: 1
 

Have you tried this?

DateTime.Now.AddHours(st)
abelLazm
Postaholic
2,113 posts since Feb 2011
Reputation Points: 219
Solved Threads: 124
 

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);
Mitja Bonca
Nearly a Posting Maven
2,485 posts since May 2009
Reputation Points: 641
Solved Threads: 474
 
DateTime TestTime = DateTime.Parse(timeString1);
TestTime = TestTime + TimeSpan.Parse(timeString2);
abelLazm
Postaholic
2,113 posts since Feb 2011
Reputation Points: 219
Solved Threads: 124
 

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.

kirtee2209
Light Poster
28 posts since Apr 2011
Reputation Points: 19
Solved Threads: 1
 

Try this way

textBox1.text=cmd.ExecuteScalar().ToString();
DateTime Staff_Total_Hours = Convert.ToDateTime(textBox1.Text) +  Convert.ToDateTime(txtTime.Text);
abelLazm
Postaholic
2,113 posts since Feb 2011
Reputation Points: 219
Solved Threads: 124
 
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'.

kirtee2209
Light Poster
28 posts since Apr 2011
Reputation Points: 19
Solved Threads: 1
 

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

Mitja Bonca
Nearly a Posting Maven
2,485 posts since May 2009
Reputation Points: 641
Solved Threads: 474
 

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?

Mitja Bonca
Nearly a Posting Maven
2,485 posts since May 2009
Reputation Points: 641
Solved Threads: 474
 
DateTime Staff_Total_Hours = (Convert.ToDateTime(textBox1.Text)).ToTimeSpan().Add(  (Convert.ToDateTime(txtTime.Text)).ToTimeSpan());


try ths

abelLazm
Postaholic
2,113 posts since Feb 2011
Reputation Points: 219
Solved Threads: 124
 

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

Mitja Bonca
Nearly a Posting Maven
2,485 posts since May 2009
Reputation Points: 641
Solved Threads: 474
 
DateTime Staff_Total_Hours = (Convert.ToDateTime(textBox1.Text)).ToTimeSpan().Add(  (Convert.ToDateTime(txtTime.Text)).ToTimeSpan());

does not solve the problem

kirtee2209
Light Poster
28 posts since Apr 2011
Reputation Points: 19
Solved Threads: 1
 

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);
abelLazm
Postaholic
2,113 posts since Feb 2011
Reputation Points: 219
Solved Threads: 124
 
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.

Mitja Bonca
Nearly a Posting Maven
2,485 posts since May 2009
Reputation Points: 641
Solved Threads: 474
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You