none
DateTime Subtract

    Question

  • hi guys

    i just wanted to find out how to, for example remove 45 day to a date I have... found ways to compare or add, but i just can find how to remove day to a certain date...~

     

    Thx

    Wednesday, May 24, 2006 11:37 AM

Answers

  • Hi there,

    To subtract a number of days, simply pass a negative value to the AddDays() function of the DateTime class. For example, to subtract 12 days you pass -12 to the function.

    A little code example:

    DateTime x = new DateTime(2006, 5, 13);

    MessageBox.Show(x.ToString());
    MessageBox.Show(x.AddDays(-12).ToString());

    The message boxes are there to demonstrate the act of subtraction. Remember that AddDays() function actually returns a new DateTime object containing the modified date.

    Hope that helps a bit, but sorry if it doesn't
    Wednesday, May 24, 2006 11:47 AM

All replies

  • Hi there,

    To subtract a number of days, simply pass a negative value to the AddDays() function of the DateTime class. For example, to subtract 12 days you pass -12 to the function.

    A little code example:

    DateTime x = new DateTime(2006, 5, 13);

    MessageBox.Show(x.ToString());
    MessageBox.Show(x.AddDays(-12).ToString());

    The message boxes are there to demonstrate the act of subtraction. Remember that AddDays() function actually returns a new DateTime object containing the modified date.

    Hope that helps a bit, but sorry if it doesn't
    Wednesday, May 24, 2006 11:47 AM
  • thx

    didn't noticed the value could be negative.

    Thank you NateV

    Wednesday, May 24, 2006 11:49 AM