[英]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 set minute to 0 and likewise 如果我增加10分钟,它将增加1小时并将分钟设置为0,同样
有AddMinutes函数。
As Darin Dimitrov mentions , there is an AddMinutes function. 正如Darin Dimitrov所提到的 ,有一个AddMinutes函数。
However, be aware that you can't just do: 但是,请注意,您不能只是这样做:
dateTime1.AddMinutes(50);
AddMinutes
returns a new DateTime , so you'll need to do: AddMinutes
返回一个新的DateTime ,因此您需要执行以下操作:
dateTime1 = dateTime1.AddMinutes(50);
You can add a TimeSpan
via .Add()
您可以通过
.Add()
添加TimeSpan
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. 您还可以
AddDays(int days)
, AddHours(int hours)
, AddMinutes(int minutes)
, AddSeconds(int seconds)
等。
All of these functions return DateTime objects so you'll have to set the value equal to the return value of the method. 所有这些函数都返回DateTime对象,因此您必须将值设置为等于方法的返回值。
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... 如果我理解您的问题,如果您只想添加分钟,则可以使用AddMinutes方法。
http://msdn.microsoft.com/en-us/library/system.datetime.addminutes.aspx http://msdn.microsoft.com/zh-CN/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. 最初应将其设置为50分钟,然后再添加10分钟将使其变为一个小时。 You may want to consider using a TimeSpan instead though.
不过,您可能要考虑使用TimeSpan。
TimeSpan span = TimeSpan.FromMinutes(50);
span += TimeSpan.FromMinutes(10);
Console.WriteLine(span.Hours); // prints "1"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.