Thursday, April 21, 2011

I want add hours or minutes to current time

Hello .. I want 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?

 (DateTime.Now.ToShortDateString()  +.......)
From stackoverflow
  • You can use other variable

    DateTime otherDate = DateTime.Now.AddMinutes(25);
    DateTime tomorrow = DateTime.Now.AddHours(25);
    
    Stormenet : Didn't know they had days of 25 hours these days :p
  • You can also add a TimeSpan to a DateTime, as in:

    date + TimeSpan.FromHours(8);
    
  • 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);
    

    For a nice overview of even more DateTime manipulations see http://www.blackwasp.co.uk/CSharpDateManipulation.aspx

0 comments:

Post a Comment