Results 1 to 8 of 8

Thread: date function

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2004
    Posts
    69

    date function

    Dim strDOB as string

    strDOB = "03/28/1963"


    How to calculate this person age using date function?


    Thank you.


    vb999

  2. #2
    Hyperactive Member dRAMmer's Avatar
    Join Date
    Oct 2001
    Location
    strangelans
    Posts
    463
    This will give you only the age in year
    Code:
    INT(DateDiff("d", "03/28/1963", Now) / 365.25)
    live, code and die...

  3. #3
    The picture isn't missing BuggyProgrammer's Avatar
    Join Date
    Oct 2000
    Location
    Vancouver, Canada
    Posts
    5,217
    Use DateDiff:

    Msgbox datediff("YYYY",strDOB,now)

    Edit: Nevermind my solution sucks.
    Last edited by BuggyProgrammer; Sep 5th, 2004 at 08:23 PM.
    Remember, if someone's post was not helpful, you can always rate their post negatively .

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jul 2004
    Posts
    69

    [RESOLVED]

    Thank you, dRAMmer.

    INT(DateDiff("d", "03/28/1963", Now) / 365.25) -- works!


    vb999

  5. #5
    Fanatic Member daydee's Avatar
    Join Date
    Jun 2001
    Location
    Canada
    Posts
    560
    The interval argument for DateDiff function has these settings:

    yyyy Year
    q Quarter
    m Month
    y Day of year
    d Day
    w Weekday
    ww Week
    h Hour
    n Minute
    s Second

    Any idea why DateDiff("yyyy", strDOB, Now) returns one extra year?

    I think this will work too:
    VB Code:
    1. Int(DateDiff("m", strDOB, Now) / 12)
    Cheers!
    Give your music collection a whole new life with PartyTime Jukebox

  6. #6
    Hyperactive Member dRAMmer's Avatar
    Join Date
    Oct 2001
    Location
    strangelans
    Posts
    463
    daydee,

    your code good, but if vb999 would encounter a leap year, it would cause a bit of problem, 365.25 is the average year, so i think he's better of using it, cheers!

    also, Datediff gives us an extra year because, as long as it encounters a different value in year it will compute for it, anyway it make sense since from Dec.31,2004 to Jan.1,2005, a year has elasped

    so for:
    Code:
    DateDiff("yyyy", "12/31/2004",01/01/2005")
    gives use 1
    live, code and die...

  7. #7
    Fanatic Member daydee's Avatar
    Join Date
    Jun 2001
    Location
    Canada
    Posts
    560

    LOL I guess since we're dividing the year of birth by 365.25 (365 days + 5 hours , 48 minutes and 46 seconds.) we should really be using the hour of birth in the equation as well!

    Like, suppose you were born February 29, 1972 at 10 PM. Then 365 and one-quarter days went by and behold, technically the first anniversary of your birth hour would come on March 1, 1973, at about 4 AM.

    The second and third anniversaries would fall on March 1, at 10 AM and 4 PM respectively.
    Comes year four (1976), and your anniversary is back where it started, February 29 at 10 PM.

    It gets worst...
    If you were born February 28, 1972, at 4 AM, you were supposed to celebrate all your non-leap-year birthdays on February 27.

    I understand now why the " / 365.25" equation is ultimately the right way to determine one's exact age but like I said...
    I think for this to be valid, we should add in the exact hour of birth as well. Does that make sense?

    Cheers!
    Give your music collection a whole new life with PartyTime Jukebox

  8. #8
    Hyperactive Member dRAMmer's Avatar
    Join Date
    Oct 2001
    Location
    strangelans
    Posts
    463
    live, code and die...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width