Subtract a date from a date

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Benniit
    New Member
    • May 2008
    • 54

    Subtract a date from a date

    I'm using vb.net 2008 and sqlserver 2008 and I want subtract a date from a specified date. Example:

    I want to subtract Commencedate from Today's Date but when I used datediff it gave me (argument "interval" is not a valid date).

    When I used year(Commenceda te)-year(now), it worked but the month did not get subtracted. Can someone help me out?

    Thank in advance,

    Ben
  • CroCrew
    Recognized Expert Contributor
    • Jan 2008
    • 564

    #2
    Hello Benniit,

    Can we get you to post your code so that we can provide you with the best solution to your question? It makes it easier to show you when we can inject the solution into your code.

    Happy Coding,
    CroCrew~

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      As long as your dates are DateTime objects you don't need to specify the years. Just subtract one from the other.

      Code:
      // C#
      DateTime start = new DateTime.TryParse("December 25, 1979");
      DateTime end = DateTime.Now;
      Timespan TheDiff = end - start;

      Comment

      Working...