I want add hours or minutes to current time

前端 未结 4 1875
小蘑菇
小蘑菇 2020-12-03 13:40

I want to increase time to current time.

for example, I have the time of the problem and the expected time to complete them
How can I add?

 (Date         


        
4条回答
  •  星月不相逢
    2020-12-03 14:11

    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
    }
    

提交回复
热议问题