tags:

views:

82

answers:

5

is there a function that would do this

DateTime1.minute=50

if i add 10 minutes it would add 1 hour and set minute to 0 and likewise

+9  A: 

There's the AddMinutes function.

Darin Dimitrov
+1 for obviousness
Paddy
+2  A: 

You can add a TimeSpan via .Add()

DateTime now = DateTime.Now;
TimeSpan tenMinutes = new TimeSpan(0, 10, 0);
now = now.Add(tenMinutes);

You can also AddDays(int days), AddHours(int hours), AddMinutes(int minutes),AddSeconds(int seconds), etc.

All of these functions return DateTime objects so you'll have to set the value equal to the return value of the method.

DateTime now = DateTime.Now;
now = now.AddMinutes(10);
Brad
This won't work. now.Add(...) returns a new DateTime, which you're currently throwing away.
Reed Copsey
@Reed, good catch. I've corrected my answer.
Brad
+1  A: 

If I understand your question, you can use the AddMinutes method if you just want to add minutes...

http://msdn.microsoft.com/en-us/library/system.datetime.addminutes.aspx

David Stratton
+6  A: 

As Darin Dimitrov mentions, there is an AddMinutes function.

However, be aware that you can't just do:

dateTime1.AddMinutes(50);

AddMinutes returns a new DateTime, so you'll need to do:

dateTime1 = dateTime1.AddMinutes(50);
Reed Copsey
+1 for pointing out DateTime is immutable.
qstarin
A: 

Or a shorter code example

DateTime dt = DateTime.AddMinutes(50);

// some other logic here

dt.AddMinutes(10);

That should initially set it to 50mins and then adding another 10mins would make it an hour. You may want to consider using a TimeSpan instead though.

TimeSpan span = TimeSpan.FromMinutes(50);
span += TimeSpan.FromMinutes(10);

Console.WriteLine(span.Hours); // prints "1"
jlafay