Calculate age / age calculator

Post your working scripts, libraries and tools for AHK v1.1 and older
skyth540
Posts: 72
Joined: 24 Aug 2022, 10:07

Calculate age / age calculator

31 Aug 2022, 17:20

Code: Select all

DOB := "02/15/1998"   ; input the birthdate   (the format is MM/DD/YYYY)


StringSplit, dates, DOB, `/    ; chops up DOB

MAct:= A_MM    ; setting variable for current month
AAct:= A_YYYY  ; setting variable for current year
DAct:= A_DD  ; setting variable for current day

MNac:= dates1  ; setting variable for birth month
ANac:= dates3   ; setting variable for birth year
DNac:= dates2  ; setting variable for birth day
sleep, 10   ; chilling for a sec
If (MAct<MNac)
{
Age:=AAct-ANac-1
}
If (MAct=MNac and DAct<DNac)
{
Age:=AAct-ANac-1
}
If (MAct=MNac and DAct>=DNac)
{
Age:=AAct-ANac
}
If (MAct>MNac)
{
Age:=AAct-ANac
}
sleep, 100     ; chilling for a sec

MsgBox, The age is %age%

A little while ago I needed a way to calculate age off of knowing a birthdate. Most documentation I found wasn't really working in my case, so after working through what was best for me, I wanted to turn around and share it to hopefully help someone else down the road. Probably not the most elegant way of doing things and it could be reduced somewhat but I think it is stable and pretty easy to understand
Last edited by BoBo on 31 Aug 2022, 23:48, edited 1 time in total.
Reason: Moved from 'Ask for Help' to 'Scripts & Functions'.
User avatar
TheDewd
Posts: 1513
Joined: 19 Dec 2013, 11:16
Location: USA

Re: Calculate age / age calculator

01 Sep 2022, 14:12

Here's another way. (Requires Internet Connection)

Code: Select all

#SingleInstance, Force

Month := "10"
Day := "28"
Year := "1955"

URL := "https://www.datetime.io/age/" Year "/" Month "/" Day

HttpReq := ComObjCreate("WinHttp.WinHttpRequest.5.1")
HttpReq.Open("GET", URL, true)
HttpReq.Send()
HttpReq.WaitForResponse()

RegExMatch(HttpReq.ResponseText, "<h2 class=""answer"">(.*?)<\/h2>", Match)
Match := RegexReplace(Match1, "^\s+|\s+$|<span>|<\/span>")
RegExMatch(Match, "(\d+) years", Year)
RegExMatch(Match, "(\d+) months", Month)
RegExMatch(Match, "(\d+) weeks", Week)
RegExMatch(Match, "(\d+) days", Day)

MsgBox, % Match

MsgBox, % "Years: " (Year1 = "" ? 0 : Year1) "`nMonths: " (Month1 = "" ? 0 : Month1) "`nWeeks: " (Week1 = "" ? 0 : Week1) "`nDays: " (Day1 = "" ? 0 : Day1)

Additionally, you can write a script to use a different URL to get the total seconds, minutes, hours, days, and weeks.

https://www.datetime.io/how-long-ago-was/1955/10/28
User avatar
rommmcek
Posts: 1479
Joined: 15 Aug 2014, 15:18

Re: Calculate age / age calculator

02 Sep 2022, 03:35

@TheDewd: Seems to be a tiny bug for 1 (one) Year/Month/Week/Day.
This works:

Code: Select all

RegExMatch(Match, "(\d+) years?", Year)
RegExMatch(Match, "(\d+) months?", Month)
RegExMatch(Match, "(\d+) (weeks?)", Week)
RegExMatch(Match, "(\d+) days?", Day)
carno
Posts: 265
Joined: 20 Jun 2014, 16:48

Re: Calculate age / age calculator

02 Sep 2022, 18:52

Code: Select all

;T1=20000101
;T2=20110228010203
; http://www.autohotkey.com/board/topic/119833-elapsed-time-calculator/?p=682909

T1 = 19770615
T2 = A_Now

/*
x := ElapsedTime(T1)
MsgBox % "Age / Duration Since " T1 "`n"
. "`n" x.Yr	" Years"
. "`n" x.Mon	" Months"
. "`n" x.Day	" Days"
. "`n" x.Hr	" Hours"
. "`n" x.Min	" Minutes"
. "`n" x.Sec	" Seconds"
*/

x := ElapsedTime(T1, T2)
MsgBox % "Your Age:`n"
;MsgBox % "Duration between " T1 "`nand " T2 "`n" 
. "`n" x.Yr	" Years"
. "`n" x.Mon	" Months"
. "`n" x.Day	" Days"
. "`n" x.Hr	" Hours"
. "`n" x.Min	" Minutes"
. "`n" x.Sec	" Seconds"
Yr := x.Yr
Mon := x.Mon
Day := x.Day
Clipboard = %Yr% Years, %Mon% Months and %Day% Days
return

ElapsedTime(T1, T2:=""){ ; http://www.autohotkey.com/board/topic/119833-elapsed-time-calculator/
	if (T1>T2)
		Tx:=T1,T1:=T2,T2:=Tx,Neg:=1
	FormatTime,T1,%T1%,yyyyMMddHHmmss
	FormatTime,T2,%T2%,yyyyMMddHHmmss
	Yr:=SubStr(T2,1,4)-(Yr1:=SubStr(T1,1,4)),Mon:=SubStr(T2,5,2)-(Mon1:=SubStr(T1,5,2)),Day:=SubStr(T2,7,2)-SubStr(T1,7,2)
	Hr:=SubStr(T2,9,2)-SubStr(T1,9,2),Min:=SubStr(T2,11,2)-SubStr(T1,11,2),Sec:=SubStr(T2,13,2)-SubStr(T1,13,2),Res:=[]
	if Sec<0
		Sec+=60,Min--
	if Min<0
		Min+=60,Hr--
	if Hr<0
		Hr+=24,Day--
	if Day<0
		Day+=(Mon1~="[469]|11")?30:Mon1=2?(Mod(Yr1,4)?28:29):31,Mon--
	if Mon<0
		Mon+=12,Yr--
	for each,v in StrSplit("Yr,Mon,Day,Hr,Min,Sec",",")
		x:=%v%*(Neg?-1:1),Res[v]:=(T1&&T2)?x:"Error"
	return Res
}

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: dogbar492, hiahkforum and 37 guests