开发者

question on DateTime addtime in c#?

is there a function that would do this

DateTime1.minute=50

if i add 10 minutes it would add 1 hour and se开发者_JS百科t minute to 0 and likewise


There's the AddMinutes function.


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);


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);


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


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"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜