Calculate Person Age in Year Months Days

Discuss RoboTask here
Post Reply
OldGeek
Posts: 49
Joined: Thu Jun 01, 2023 2:50 pm
Location: Upstate NY

Calculate Person Age in Year Months Days

Post by OldGeek »

I do a lot of personal genealogy research so I'm trying to find a person's age in Years, Months, Days

I've tried to make a task but I'm struggling. Hoping I could get some help
Windows 11 Pro, RoboTask v10.5
Oleg
Site Admin
Posts: 3256
Joined: Thu Jan 01, 1970 1:00 am
Contact:

Re: Calculate Person Age in Year Months Days

Post by Oleg »

Time difference ?
It can give you the difference between the current moment ({date}) and a person's birthday in days as an integer value.
To get the number of years, you can use VB evaluate (integer division): {MyDays} \ 365
To get the number of months, use {MyDays} \ 30.147 (30.147 - is the average amount of days in a month)

Another way:
You can write small script and calculate time difference with any interval
Take a look at VB function DateDiff
Here is an example:

Code: Select all

;*****************************
;* RoboTask Task file
;* Do not edit in text editor!
;*****************************
 
[Root]
ActionAfterRun=INTEGER|0
Actions=FOLDER
Automat=INTEGER|-1
CatID=INTEGER|307868550
Comment=STRINGLIST
ContinueOnError=INTEGER|0
DisableOnError=INTEGER|0
DoNotStopWhenShutdown=INTEGER|0
ExternalName=STRING|"Task1572"
Hide=INTEGER|0
ID=INTEGER|2020749089
LogOnAsUser=INTEGER|1
Name=STRING|"Calculate differences"
OnErrorTaskID=INTEGER|0
Priority=INTEGER|3
RestrictRESTAPIAccess=INTEGER|0
RunOnClose=INTEGER|0
RunOnStartup=INTEGER|0
StepPause=INTEGER|0
ToLog=INTEGER|3
UnicodeFormat=INTEGER|1
WriteGeneralLog=INTEGER|0

[Actions]
Action1=FOLDER
Action2=FOLDER
Action3=FOLDER
Action4=FOLDER
Action5=FOLDER

[Actions\Action1]
ActionID=STRING|"A_VARIABLES_SET"
Enabled=INTEGER|-1
Name=STRING|"Set variable ""_birthday"" with value ""{FormatToDateTime(2014-02-19,yyyy-mm-dd)}"""
Params=FOLDER

[Actions\Action1\Params]
_rt_variables_produced=STRING|"_birthday"
expand=STRING|"1"
linecount=STRING|"1"
varname=STRING|"_birthday"
varvalue=STRING|"{FormatToDateTime(2014-02-19,yyyy-mm-dd)}"

[Actions\Action2]
ActionID=STRING|"A_SCRIPT_VBSCRIPT"
Enabled=INTEGER|-1
Name=STRING|"VB Script"
Params=FOLDER

[Actions\Action2\Params]
expandvars=STRING|"0"
line00000000=STRING|"dt1 = cdate(RoboTaskApp.expandtext(""{_birthday}""))"
line00000001=STRING|"dt2 = Date"
line00000003=STRING|"y_diff = DateDiff(""yyyy"", dt1, dt2)"
line00000004=STRING|"m_diff = DateDiff(""m"", dt1, dt2)"
line00000005=STRING|"d_diff = DateDiff(""d"", dt1, dt2)"
line00000006=STRING|"'LogMessage(""Years : "" + cstr(y_diff))"
line00000007=STRING|"'LogMessage(""Months: "" + cstr(m_diff))"
line00000008=STRING|"'LogMessage(""Days  : "" + cstr(d_diff))"
line00000009=STRING|"RoboTaskApp.setUserVariable ""_years"", cstr(y_diff)"
line0000000A=STRING|"RoboTaskApp.setUserVariable ""_months"", cstr(m_diff)"
line0000000B=STRING|"RoboTaskApp.setUserVariable ""_days"", cstr(d_diff)"
linecount=STRING|"13"
savescript=STRING|"0"

[Actions\Action3]
ActionID=STRING|"A_ROBOTASK_LOG"
Enabled=INTEGER|-1
Name=STRING|"Log message (neutral) ""Years: {_years}"""
Params=FOLDER

[Actions\Action3\Params]
expression=STRING|"3"
message=STRING|"Years: {_years}"
mode=STRING|"0"
type=STRING|"3"

[Actions\Action4]
ActionID=STRING|"A_ROBOTASK_LOG"
Enabled=INTEGER|-1
Name=STRING|"Log message (neutral) ""Months: {_months}"""
Params=FOLDER

[Actions\Action4\Params]
expression=STRING|"3"
message=STRING|"Months: {_months}"
mode=STRING|"0"
type=STRING|"3"

[Actions\Action5]
ActionID=STRING|"A_ROBOTASK_LOG"
Enabled=INTEGER|-1
Name=STRING|"Log message (neutral) ""Days: {_days}"""
Params=FOLDER

[Actions\Action5\Params]
expression=STRING|"3"
message=STRING|"Days: {_days}"
mode=STRING|"0"
type=STRING|"3"
Oleg Yershov
OldGeek
Posts: 49
Joined: Thu Jun 01, 2023 2:50 pm
Location: Upstate NY

Re: Calculate Person Age in Year Months Days

Post by OldGeek »

Thank you, this is a good start. What I'm looking for is way to calculate an age like 25 years, 3 months and 2 days. Not total months or total days
I'm trying to calculate someone's age at the time of their death ex 25 years, 3 months and 2 days
Windows 11 Pro, RoboTask v10.5
Oleg
Site Admin
Posts: 3256
Joined: Thu Jan 01, 1970 1:00 am
Contact:

Re: Calculate Person Age in Year Months Days

Post by Oleg »

I'm trying to calculate someone's age at the time of their death ex 25 years, 3 months and 2 days
The algorithm will be a little more complicated.
Take a look at this example.

Code: Select all

;*****************************
;* RoboTask Task file
;* Do not edit in text editor!
;*****************************
 
[Root]
ActionAfterRun=INTEGER|0
Actions=FOLDER
Automat=INTEGER|0
CatID=INTEGER|757834839
Comment=STRINGLIST
ContinueOnError=INTEGER|0
DisableOnError=INTEGER|0
DoNotStopWhenShutdown=INTEGER|0
ExternalName=STRING|"Task68"
Hide=INTEGER|0
ID=INTEGER|1456495319
LogOnAsUser=INTEGER|1
Name=STRING|"Calculate age - forum"
OnErrorTaskID=INTEGER|0
Priority=INTEGER|3
RestrictRESTAPIAccess=INTEGER|0
RunOnClose=INTEGER|0
RunOnStartup=INTEGER|0
StepPause=INTEGER|0
ToLog=INTEGER|3
UnicodeFormat=INTEGER|1
WriteGeneralLog=INTEGER|0

[Actions]
Action1=FOLDER
Action2=FOLDER
Action3=FOLDER

[Actions\Action1]
ActionID=STRING|"A_VARIABLES_SET"
Enabled=INTEGER|-1
Name=STRING|"Set variable ""_birthday"" with value ""{FormatToDateTime(2014-02-19,yyyy-mm-dd)}"""
Params=FOLDER

[Actions\Action1\Params]
_rt_variables_produced=STRING|"_birthday"
expand=STRING|"1"
linecount=STRING|"1"
varname=STRING|"_birthday"
varvalue=STRING|"{FormatToDateTime(2014-02-19,yyyy-mm-dd)}"

[Actions\Action2]
ActionID=STRING|"A_SCRIPT_VBSCRIPT"
Enabled=INTEGER|-1
Name=STRING|"VB Script"
Params=FOLDER

[Actions\Action2\Params]
expandvars=STRING|"0"
line00000000=STRING|"Function CalculateAge(birthDate)"
line00000001=STRING|"    Dim currentDate"
line00000002=STRING|"    Dim years, months, days"
line00000003=STRING|"    Dim birthYear, birthMonth, birthDay"
line00000004=STRING|"    Dim currentYear, currentMonth, currentDay"
line00000006=STRING|"    currentDate = Date"
line00000008=STRING|"    birthDate = CDate(birthDate)"
line0000000A=STRING|"    birthYear = Year(birthDate)"
line0000000B=STRING|"    birthMonth = Month(birthDate)"
line0000000C=STRING|"    birthDay = Day(birthDate)"
line0000000E=STRING|"    currentYear = Year(currentDate)"
line0000000F=STRING|"    currentMonth = Month(currentDate)"
line00000010=STRING|"    currentDay = Day(currentDate)"
line00000012=STRING|"    years = currentYear - birthYear"
line00000013=STRING|"    months = currentMonth - birthMonth"
line00000014=STRING|"    days = currentDay - birthDay"
line00000016=STRING|"    If days < 0 Then"
line00000017=STRING|"        months = months - 1"
line00000019=STRING|"        If currentMonth = 1 Then"
line0000001A=STRING|"            days = Day(DateSerial(currentYear - 1, 12 + 1, 0)) + currentDay - birthDay"
line0000001B=STRING|"        Else"
line0000001C=STRING|"            days = Day(DateSerial(currentYear, currentMonth, 0)) + currentDay - birthDay"
line0000001D=STRING|"        End If"
line0000001E=STRING|"    End If"
line00000020=STRING|"    If months < 0 Then"
line00000021=STRING|"        years = years - 1"
line00000022=STRING|"        months = months + 12"
line00000023=STRING|"    End If"
line00000025=STRING|"    RoboTaskApp.SetuserVariable ""_years"", cstr(years)"
line00000026=STRING|"    RoboTaskApp.SetuserVariable ""_months"", cstr(months)"
line00000027=STRING|"    RoboTaskApp.SetuserVariable ""_days"", cstr(days)"
line00000029=STRING|"    CalculateAge = years & ""-"" & Right(""0"" & months, 2) & ""-"" & Right(""0"" & days, 2)"
line0000002A=STRING|"End Function"
line0000002C=STRING|"dt = Cdate(RoboTaskApp.expandText(""{_birthday}""))"
line0000002D=STRING|"LogMessage(CalculateAge(dt))"
linecount=STRING|"47"
savescript=STRING|"0"

[Actions\Action3]
ActionID=STRING|"A_ROBOTASK_LOG"
Enabled=INTEGER|-1
Name=STRING|"Log message (neutral) ""Age is: {_years} years, {_mont..."""
Params=FOLDER

[Actions\Action3\Params]
expression=STRING|"3"
message=STRING|"Age is: {_years} years, {_months} months, {_days} days"
mode=STRING|"0"
type=STRING|"3"

Oleg Yershov
OldGeek
Posts: 49
Joined: Thu Jun 01, 2023 2:50 pm
Location: Upstate NY

Re: Calculate Person Age in Year Months Days

Post by OldGeek »

Thank you, Thank you. It works perfectly
Windows 11 Pro, RoboTask v10.5
Post Reply