Originally posted by Chad Ward
I'm trying to get the age of a person in either years months or days, this tcl is working corrrect if more then a year old, but when you get under 1 year I'm running into a problem. it is working if the month from last year is less then 9 but when the month gets to 10 / 11 / or 12 it gives me 1M / 2M / 3M. (birthday is 20051013 and current date is 20060918) which should return a 11M. Could someone take a look at this code and let me know what I did wrong. Thanks
# Set array of days in a month
array set dom
- # Assume birthdate in format YYYYMMDD
set birthday $xlateInVals
# Get today's date in YYYYMMDD
set now [clock format [clock seconds] -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}]
# set age in either years month or days
set Y Y
set M M
set D D
if {$years > 0} {
set xlateOutVals $years$Y
} else {
if {$months > 0} {
set xlateOutVals $months$M
} else {
if {$days > 0} {
set xlateOutVals $days$D
} else {
set xlateOutVals N/A}
}
}