35

I have my TimeSpan for a specific reason so it HAS to be in that format. I'm trying to add an hour on to the current time. Here is what I got, which does not work:

TimeSpan time1= TimeSpan.FromHours(1); // my attempt to add 2 hours
TimeSpan ts = DateTime.Now.TimeOfDay;
ts.Add(time1);
MessageBox.Show(ts.ToString()); // for showing me its result

Can you please advise?

0

9 Answers 9

33
ts += TimeSpan.FromHours(1);

and there you have it!

26

The method Add of TimeSpan is not modifying the value of ts. It is summing the values and returning a new object.

So instead you should do:

 TimeSpan ts = DateTime.Now.TimeOfDay;
 var ts2 = ts.Add(time1);
 MessageBox.Show(ts2.ToString());
2
  • Works perfect with my code. Thanks very much. My bad I understand now.
    – Jeremy
    Sep 11, 2013 at 16:42
  • @Jeremyc Operations with TimeSpan and DateTime usually return new objects. Glad it worked!
    – margabit
    Sep 11, 2013 at 16:44
20

The reason why your code does not work is that TimeSpan is immutable. The TimeSpan.Add method returns a new object:

ts = ts.Add(time1);
2
  • 2
    +1 for using the word 'immutable' as this is the important bit. Sep 13, 2013 at 16:02
  • Wondering why didn't they make it static then?? (just saying) May 23, 2019 at 11:58
5

Use AddHours();

DateTime.Now.AddHours(1);

or, to use your code (see the newTs variable)..

TimeSpan time1 = TimeSpan.FromHours(1); // my attempt to add 2 hours
TimeSpan ts = DateTime.Now.TimeOfDay;
var newTs = ts.Add(time1);
MessageBox.Show(newTs.ToString());

Also, your comment says 'my attempt to add 2 hours', is this a typo?

2
  • why would it be a typo
    – Jeremy
    Sep 11, 2013 at 17:24
  • 4
    @Jeremyc, you mention 2 hours, but you have 1 in the parenthesis. Sep 11, 2013 at 18:27
4

My 2 cents: The question title says - How to add an hour to a timespan. C#, only one answer is accurate to that effect. Here's my take on adding hours to a time span:

public static TimeSpan AddHours(TimeSpan timeSpan, int hoursToAdd)
{
   TimeSpan newSpan = new TimeSpan(0, hoursToAdd, 0, 0);
   return timeSpan.Add(newSpan);
}

Here's a fiddle to Demo: https://dotnetfiddle.net/iuh66s

3
var newDateTime = DateTime.Now.AddHours(1);

No need to create a TimeSpan. This will roll over to tomorrow.

4
  • I need it to be a timespan using TimeSpan ts = DateTime.Now.TimeOfDay;
    – Jeremy
    Sep 11, 2013 at 16:36
  • Why? It's not clear from your question why you need it as a time span. Sep 11, 2013 at 16:36
  • what if it rolls over 24 hours? Sep 11, 2013 at 16:37
  • 1
    @Jeremyc you can compare datetimes. Sep 11, 2013 at 16:38
2

Try using this:-

TimeSpan time1 = TimeSpan.FromHours(1); // my attempt to add 2 hours
TimeSpan ts = DateTime.Now.TimeOfDay;
var newts = ts.Add(time1);
MessageBox.Show(newts.ToString());

Try this:-

var newDateTime = DateTime.Now.AddHours(1);
1
var newTime = oldTime.Add(new TimeSpan(1, 0, 0));

Don't fall into the common mistake of not assigning the value to anything (i.e. newTime) as oldTime will remain the same - TimeSpan.Add(...) returns TimeSpan.

Same applies for String.Replace(...) - easy, but devastating, to miss.

0

if you need to add minutes to time and if time is variable and not constant

TimeSpan firstTime = TimeSpan.Parse(firstTime);
TimeSpan NeededTimeForAdd= TimeSpan.FromMinutes(SessionMinutes);    
TimeSpan ExpectedTime = firstTime.Add(NeededTime);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.