Page 1 of 1

DateToNum and leap year

PostPosted: Wed Jan 11, 2017 12:43 pm
by BRobinsonS
I wanted to write a small routine to determine the age of a person.

Code: Select all
DateToNum "[DateFld]" "Default" "[NumResult]"
DateToNum "[DateShort]" "Default" "[Today]"
Math "[Today]-[NumResult]" "0" "[Result]"

Math "[Result]/365" "1" "[Result2]"

SearchStr "." "[Result2]" "[PosOfPeriod]" ""
StrDel "[Result2]" "3" "2" "[RemoveDec]"
AlertBox "Answer" "Person is [RemoveDec] years old born [DateFld]"

Then it occurred to me that dividing by 365 may not be give the desired results because of leap years.

Re: DateToNum and leap year

PostPosted: Wed Jan 11, 2017 4:31 pm
by Gaev

It is not that simple.

But first, are you just looking for whole number of years ? ... i.e. 47.7 years translates to 47 years ? ... or 48 ?

In any case, your best approach is to compare the "month+day" values for the following ranges ...

- value less than -187
- value between -186 and +186
- value greater than 187

... then, adjust the year difference accordingly

Warning, this might still cause problems when comparing February/29 (of a leap year) vs. March/1 of a non-leap year.

Re: DateToNum and leap year

PostPosted: Wed Jan 11, 2017 4:42 pm
by virger
Try this:
Code: Select all
Math "[Result]/365.25" "1" "[Result2]"

Math "[Result]/365.25" "1" "[Result2]"

Re: DateToNum and leap year

PostPosted: Wed Jan 11, 2017 4:53 pm
by Rasl
I use date format dd/mm/yyyy

StrParse "[DateShort]" "/" "[hoy]" "[x]"
StrParse "[FechaNac]" "/" "[Nac]" "[y]"
Math "[hoy3]-[nac3]" "0" "[anios]"
Math "[hoy2]-[nac2]" "0" "[meses]"
Math "[hoy1]-[nac1]" "0" "[dias]"
If "[meses]" "<" "0"
Math "[anios]-1" "0" "[edad]"
If "[dias]" "<" "0"
Math "[anios]-1" "0" "[edad]"
SetVar "[edad]" "[anios]"


Re: DateToNum and leap year

PostPosted: Wed Jan 11, 2017 5:29 pm
by BRobinsonS
Thanks for the responses.

Gaev, I rounded the answer to 1 decimal place so if it rounded up that would happen.
I used my own example. My birthday is in April therefore, I am not one year older until then.
So I don't want it rounded up. If I was after April then it should round up.

After looking into this on my own, the simplest and best is to use
Code: Select all
Math "[Result]/365.25" "1" "[Result2]"

Thanks Virger for suggesting.

Rasl : not sure what some of the variables represent i.e. [hoy] etc.

I was going to do something more elaborate and determine the number of leap years with occurred between dates and add extra day for each leap year.
But I think using the estimate of 365.25 days per year to account for leap years will work for the estimate I need.
This is determining how old a person was when he/she died for a form I use in my software.

Thanks again.

Re: DateToNum and leap year

PostPosted: Wed Jan 11, 2017 5:55 pm
by virger
A) Hoy == Today
B) 365.25, A year it's 365 dias and 6 hours (year leap every 4 years)
C) A function (javascript)
Code: Select all
{NeoBook Function}
Param=[%ymd]|Variable|BirthDay (yyyy/mm/dd)
var ymd = nbGetVar("[%ymd]");
var FecNace = new Date(ymd);
var Hoy = new Date();
var delta = Hoy-FecNace;
var anos  = Math.floor(delta/31557600000); // 365.25*24*60*60*1000


Re: DateToNum and leap year

PostPosted: Thu Jan 12, 2017 8:45 am
by David de Argentina

take a look of this: ... tediff.htm

Based on the NeoThinBasic Plugin

see: viewforum.php?f=34

My hyper devaluated cent,

David de Argentina

Re: DateToNum and leap year

PostPosted: Sat Jan 14, 2017 9:59 am
by PaulTomo
looks like you have probably worked out the solution.

But here is an example I put in a pub just for fun a while back that takes finding someone's age a little further.

Create basic pub add button ion the middle and paste in the code below.

Code: Select all
InputBox "Date of Birth" "Enter a persons Date of Birth to see their age In:|Years, Months, Weeks, Days, Hours and Minutes.||Results assume time of birth was at Midnight!||Example for DOB Format = 03/05/1972" "[BirthDate]"

SetVar "[StartDate]" "![BirthDate]"

..... Lets Do The Calc
StrParse "![DateShort]" "/" "[To]" "[cnt]"
SetVar "[Tyyyy]" "[To3]"
SetVar "[Tmm]" "[To2]"
SetVar "[Tdd]" "[To1]"

StrParse "![StartDate]" "/" "[From]" "[cnt]"
SetVar "[Fyyyy]" "[From3]"
SetVar "[Fmm]" "[From2]"
SetVar "[Fdd]" "[From1]"

DateToNum "[StartDate]" "d/m/y" "[StartDays]"
DateToNum "[DateShort]" "d/m/y" "[TodaysDays]"

.Assuming the from-date is in variables [Fyyyy] , [Fmm] & [Fdd] & the to-date is in [Tyyyy] , [Tmm] & [Tdd] , how about ....

.we first calc difference in months

Math "([Tyyyy]*12)+[Tmm]-([Fyyyy]*12)-[Fmm]" "0" "[DiffMonths]"

. depending on dd values, subtract one from month difference if required
If "[Tdd]" "<" "[Fdd]"
   SetVar "[DiffMonths]" "-1+[DiffMonths]"

. and back to age in years
If "[DiffMonths]" "<" "0"
   SetVar "[AgeYear]" "Negative !"
    Math "([DiffMonths]/12)-0.5" "0" "[AgeYear]"

.....can also then work out days, weeks etc.

.calc difference in days
Math "[TodaysDays]-[StartDays]" "" "[DiffDays]"

.calc difference in hours
Math "([TodaysDays]-[StartDays])*24" "" "[DiffHours]"
Math "[DiffHours]+[Hour]" "" "[DiffHours]"

Math "([DiffHours]*60)+[Minute]" "" "[DiffMins]"

.calc difference in weeks
Math "[DiffDays]/7" "" "[DiffWeeks]"
If "[Tdd]" ">" "[Fdd]"
   Math "[DiffWeeks]-1" "" "[DiffWeeks]"
AlertBox "Age Results ..." "Assumeing time of birth was|at Midnight on [BirthDate]||These are the Age Results by:||Year[#9]= [AgeYear]|Month[#9]= [DiffMonths]|Week[#9]= [DiffWeeks]|Day[#9]= [DiffDays]|Hour[#9]= [DiffHours]|Mins[#9]= [DiffMins]"

It may not help but was fun to see how to work out a persons age the in different ways.

Re: DateToNum and leap year

PostPosted: Sat Jan 14, 2017 1:48 pm
by Gaev

I like your solution the best ... other than some obscure cases around Feb/29 - March/1 and leap/non-leap years, it will give an accurate reading of yy years, mm months and dd days.

Re: DateToNum and leap year

PostPosted: Tue Jan 17, 2017 2:47 pm
by BRobinsonS
Tried for the fun of it but get Errors on Week, Day, Hour and Minutes.
But that's OK. :)

Re: DateToNum and leap year

PostPosted: Thu Jan 19, 2017 4:52 pm
by PaulTomo
Hi Brian

I guess you got the errors because you used a mm/dd/yyyy date format, when it's expecting it to be entered as dd/mm/yyyy.

Hopefully you managed to get things sorted.