Reply To: Calculating Age

Homepage Clovertech Forums Read Only Archives Cloverleaf Cloverleaf Calculating Age Reply To: Calculating Age

#57013
Charlie Bursell
Participant

Here is a quick and dirty I threw together.  Didn’t do a lot of testing

# Set array of days in a month

array set dom

    -format “%Y %m %d”]

    # Separate out YYYY, MM, and DD for both the birthday and now

    regexp — {(….)(..)(..)} $birthday {} byr bmo bdy

    lassign $now nyr nmo ndy

    # Remove leading zeros so we can do math

    foreach var “byr bmo bdy nyr nmo ndy” {

       set $var [string trimleft [set $var] 0]

    }

    # Compute days

    # If Birthday days > now days the number of days is

    # 0f days in last month – birthdate + now days

    # also must take 1 away from months but remember 0 – 12

    if {$bdy > $ndy} {

       incr nmo -1

       # if 0 or less set to 12 and decrement year

       if {$nmo <= 0} {        set nmo 12        incr nyr -1    }    set nodays $dom($nmo)    # Adjust for leap year if required    if {$nmo == 2 && [expr {$nyr %4}]} {incr nodays}    set days [expr {$nodays – $bdy + $ndy}] } else {    set days [expr {$ndy – $bdy}] } # Compute months if {$bmo > $nmo} {

       set months [expr {$bmo – $nmo}]

       incr nyr -1

    } else {

       set months [expr {$nmo – $bmo}]

    }

    set years [expr {$nyr – $byr}]

    echo AGE: $years Years, $months Months and $days Days

    OK, all of you Tcl’ers.  What did I forget here  8)

    Charlie

    Forum Statistics

    Registered Users
    5,116
    Forums
    28
    Topics
    9,291
    Replies
    34,426
    Topic Tags
    286
    Empty Topic Tags
    10