Source language: Translate to:

DateToNum and leap year

General questions about NeoBook

Moderator: Neosoft Support

DateToNum and leap year

Postby BRobinsonS » Wed Jan 11, 2017 12:43 pm

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.
Thoughts?
Brian Robinson
'When all else fails, try again!'
www.ComputerSoftwareSystems.com
User avatar
BRobinsonS
 
Posts: 335
Joined: Sun Sep 25, 2005 9:00 pm
Location: Ontario, Canada

Re: DateToNum and leap year

Postby Gaev » Wed Jan 11, 2017 4:31 pm

Brian:

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.
User avatar
Gaev
 
Posts: 3696
Joined: Fri Apr 01, 2005 7:48 am
Location: Toronto, Canada

Re: DateToNum and leap year

Postby virger » Wed Jan 11, 2017 4:42 pm

Try this:
Code: Select all
Math "[Result]/365.25" "1" "[Result2]"


Math "[Result]/365.25" "1" "[Result2]"
COSTA RICA
PURA VIDA
User avatar
virger
 
Posts: 477
Joined: Mon Sep 18, 2006 12:21 pm
Location: Costa Rica, America Central

Re: DateToNum and leap year

Postby Rasl » Wed Jan 11, 2017 4:53 pm

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]"
Else
If "[dias]" "<" "0"
Math "[anios]-1" "0" "[edad]"
Else
SetVar "[edad]" "[anios]"
EndIf
EndIf

salu2
Rasl
 
Posts: 129
Joined: Sat Apr 02, 2005 8:25 am
Location: Buenos Aires, Argentina

Re: DateToNum and leap year

Postby BRobinsonS » Wed Jan 11, 2017 5:29 pm

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.
Brian Robinson
'When all else fails, try again!'
www.ComputerSoftwareSystems.com
User avatar
BRobinsonS
 
Posts: 335
Joined: Sun Sep 25, 2005 9:00 pm
Location: Ontario, Canada

Re: DateToNum and leap year

Postby virger » Wed Jan 11, 2017 5:55 pm

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}
Version=5,80
Language=JScript
Param=[%ymd]|Variable|BirthDay (yyyy/mm/dd)
Param=[%ret]|Variable|Age
{End}
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
nbSetVar("[%ret]",anos);

Enjoy
COSTA RICA
PURA VIDA
User avatar
virger
 
Posts: 477
Joined: Mon Sep 18, 2006 12:21 pm
Location: Costa Rica, America Central

Re: DateToNum and leap year

Postby David de Argentina » Thu Jan 12, 2017 8:45 am

Or...

take a look of this:

http://www.thinbasic.org/public/applica ... tediff.htm

Based on the NeoThinBasic Plugin

see: viewforum.php?f=34

My hyper devaluated cent,

David de Argentina
User avatar
David de Argentina
 
Posts: 1543
Joined: Mon Apr 04, 2005 4:13 pm
Location: Buenos Aires, Argentina

Re: DateToNum and leap year

Postby PaulTomo » Sat Jan 14, 2017 9:59 am

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]"
EndIf

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

.....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]"
EndIf
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.
User avatar
PaulTomo
 
Posts: 62
Joined: Tue Apr 28, 2009 1:15 am
Location: UK

Re: DateToNum and leap year

Postby Gaev » Sat Jan 14, 2017 1:48 pm

Paul:

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.
User avatar
Gaev
 
Posts: 3696
Joined: Fri Apr 01, 2005 7:48 am
Location: Toronto, Canada

Re: DateToNum and leap year

Postby BRobinsonS » Tue Jan 17, 2017 2:47 pm

Tried for the fun of it but get Errors on Week, Day, Hour and Minutes.
But that's OK. :)
Brian Robinson
'When all else fails, try again!'
www.ComputerSoftwareSystems.com
User avatar
BRobinsonS
 
Posts: 335
Joined: Sun Sep 25, 2005 9:00 pm
Location: Ontario, Canada

Re: DateToNum and leap year

Postby PaulTomo » Thu Jan 19, 2017 4:52 pm

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.
User avatar
PaulTomo
 
Posts: 62
Joined: Tue Apr 28, 2009 1:15 am
Location: UK


Return to General NeoBook Discussions

Who is online

Users browsing this forum: No registered users and 1 guest