DateDiff

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • wideasleep
    New Member
    • May 2007
    • 72

    DateDiff

    Hello Everyone,

    I have what I think is a simple question concerning DateDiff. I have a form that I have the difference based on a incident date and date of birth to calculate someones age at the time of the incident. Everything works great except that I'm getting a negative number for the age. Here's the code.

    Code:
     =DateDiff("yyyy",[DateofIncident],[DOB])
    Thank you.
  • damonreid
    Recognized Expert New Member
    • Jul 2007
    • 114

    #2
    Try changing around the dates
    [code=vb]=DateDiff("yyyy ",[DOB],[DateofIncident])[/code]

    Comment

    • wideasleep
      New Member
      • May 2007
      • 72

      #3
      Originally posted by damonreid
      Try changing around the dates
      [code=vb]=DateDiff("yyyy ",[DOB],[DateofIncident])[/code]
      Thanks but been there done that one. Same outcome. The math is right on it just with the negative sign.

      Comment

      • wideasleep
        New Member
        • May 2007
        • 72

        #4
        Originally posted by wideasleep
        Thanks but been there done that one. Same outcome. The math is right on it just with the negative sign.
        Woops just went to double check myself on that and it did work. Not sure why it didn't do it in the first place when I did it.

        Thank you

        Comment

        • damonreid
          Recognized Expert New Member
          • Jul 2007
          • 114

          #5
          No problem glad it worked.

          I think you can use absolute values in access as well
          [code=vb]Abs(number)[/code]

          if this hadn't worked.

          Comment

          • missinglinq
            Recognized Expert Specialist
            • Nov 2006
            • 3533

            #6
            damonreid's code will give you your positive age, but the code won't necessarily give you an accurate age! The code you started with, and Damon modified, only gives you the difference in YEARS between the two dates, not taking into account whether or not the person involved has yet to have a birthyear this year! This code will overcome this difficulty:

            [CODE=vb]AgeAtTimeOfInci dent = DateDiff("yyyy" , [DOB],DateofIncident ) - IIf(Format$(Dat eofIncident, "mmdd") < Format$([DOB], "mmdd"), 1, 0)[/CODE]

            Linq ;0)>

            Comment

            • wideasleep
              New Member
              • May 2007
              • 72

              #7
              Originally posted by missinglinq
              damonreid's code will give you your positive age, but the code won't necessarily give you an accurate age! The code you started with, and Damon modified, only gives you the difference in YEARS between the two dates, not taking into account whether or not the person involved has yet to have a birthyear this year! This code will overcome this difficulty:

              [CODE=vb]AgeAtTimeOfInci dent = DateDiff("yyyy" , [DOB],DateofIncident ) - IIf(Format$(Dat eofIncident, "mmdd") < Format$([DOB], "mmdd"), 1, 0)[/CODE]

              Linq ;0)>
              Thats a good point! I'll give the code a try. This actually should be that specific since it has to do with our plant safety issues and tracking them.

              Comment

              • wideasleep
                New Member
                • May 2007
                • 72

                #8
                OK that worked great and with some quick math was able to verify that it's more acurate that what I had. Now since I'm a newbie I gotta look into some of the code to break it down to understand it. Mainly the FORMAT$ if anyone would like to give a quick explaination of it, otherwise I'll be looking it up.

                Thanks again for the code!

                Comment

                • wideasleep
                  New Member
                  • May 2007
                  • 72

                  #9
                  Actually I see the reasoning behind it as I look at it more. It's just how it all works together in the code.

                  Comment

                  • wideasleep
                    New Member
                    • May 2007
                    • 72

                    #10
                    Hi All,

                    I guess i wasn't ready to put this one to bed quite yet. I need to break this out by months also and I really haven't had any luck with it. I made another text field called months for it but I'm getting a number that's way off the mark.

                    Comment

                    Working...