To add time, you add the hours together, then you add the minutes together. Because there are only 60 minutes in an hour, you cannot have a time whose minute value is greater than 60. In this case, subtract 60 minutes and add 1 more to the hour.
The formula for time is given as [Time = Distance ÷ Speed].
You can use other variables:
DateTime otherDate = DateTime.Now.AddMinutes(25);
DateTime tomorrow = DateTime.Now.AddHours(25);
You can use the operators +
, -
, +=
, and -=
on a DateTime with a TimeSpan argument.
DateTime myDateTime = DateTime.Parse("24 May 2009 02:19:00");
myDateTime = myDateTime + new TimeSpan(1, 1, 1);
myDateTime = myDateTime - new TimeSpan(1, 1, 1);
myDateTime += new TimeSpan(1, 1, 1);
myDateTime -= new TimeSpan(1, 1, 1);
Furthermore, you can use a set of "Add" methods
myDateTime = myDateTime.AddYears(1);
myDateTime = myDateTime.AddMonths(1);
myDateTime = myDateTime.AddDays(1);
myDateTime = myDateTime.AddHours(1);
myDateTime = myDateTime.AddMinutes(1);
myDateTime = myDateTime.AddSeconds(1);
myDateTime = myDateTime.AddMilliseconds(1);
myDateTime = myDateTime.AddTicks(1);
myDateTime = myDateTime.Add(new TimeSpan(1, 1, 1));
For a nice overview of even more DateTime manipulations see THIS
You can also add a TimeSpan to a DateTime, as in:
date + TimeSpan.FromHours(8);
Please note that you may add - (minus) sign to find minutes backwards
DateTime begin = new DateTime();
begin = DateTime.ParseExact("21:00:00", "H:m:s", null);
if (DateTime.Now < begin.AddMinutes(-15))
{
//if time is before 19:45:00 show message etc...
}
and time forward
DateTime end = new DateTime();
end = DateTime.ParseExact("22:00:00", "H:m:s", null);
if (DateTime.Now > end.AddMinutes(15))
{
//if time is greater than 22:15:00 do whatever you want
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With