Hot Topic (More than 10 Replies) Problem with checking age. (Read 1904 times)
alex_smits
Member
*
Offline


No personal text

Posts: 27
Location: Arnhem, The Netherlands
Joined: Dec 15th, 2003
Problem with checking age.
Jul 10th, 2008 at 12:54pm
Print Post Print Post  
I have to check on a specific date the age in months.

I have this code

GLOBAL CODE
// Returns a date that is vMonths from vStartDate
// Use negative vMonths to get a date before vStartDate
FUNCTION GetDateByMonths(vStartDate as Date, vMonths as Int) as Date
var vRet as Date
var vYear as Int
var vDay as Int
var vMonth as Int
var vOffset as Int
var vDir as Int // Add (1) or subtract (-1) months

vRet = vStartDate
vDir = 1
vOffset = vMonths

// Split date into pieces
vYear = @Int(@Year(vStartDate))
vDay = @DOM(vStartDate)
vMonth = @Month(vStartDate)

// Work with a positive number of months
If vOffset < 0
{
vDir = -1
vOffset = @Abs(vOffset)
}

// Adjust months
If vDir = 1
{
vMonth = vMonth + vOffset

// Adjust for multiple years
While vMonth > 12
{
vYear = vYear + 1
vMonth = vMonth - 12
}
}
Else
{
vMonth = vMonth - vOffset

// Adjust for multiple years
While vMonth < 1
{
vYear = vYear - 1
vMonth = vMonth + 12
}
}

// Assemble new date
vRet = @Str(vYear) + "/" + @Text(2 - @Len(@Str(vMonth)), "0") + @Str(vMonth) + "/" + @Text(2 - @Len(@Str(vDay)), "0") + @Str(vDay)
Return vRet

END FUNCTION


and

Element Leeftijd0 On element entry

var vBirthdate as Date
var vFirst as Date
var vLast as Date

if klasse = 1 then
{
vBirthdate = GEB_D
vFirst = GetDateByMonths(datum_keuring, -6)    // Get date 6 months before Datum keuring
vLast = GetDateByMonths(datum_keuring, -9)     // Get date 9 months before Datum keuring
If (vBirthdate >= vFirst) And (vBirthdate <= vLast)
{
@MSG("Good Dog!")
}
Else
{
WriteLn("Bad Dog!")
}
}

else if klasse = 2 then
{
vBirthdate = GEB_D
vFirst = GetDateByMonths(datum_keuring, -15)    // Get date 15 months before Datum keuring
vLast = GetDateByMonths(datum_keuring, -9999)     // Get date 9999 months before Datum keuring
If (vBirthdate >= vFirst) And (vBirthdate <= vLast)
{
@MSG("Good Dog!")
}
Else
{
WriteLn("Bad Dog!")
}
}

else if klasse = 3 then
{
vBirthdate = GEB_D
vFirst = GetDateByMonths(datum_keuring, -18)    // Get date 18 months before Datum keuring
vLast = GetDateByMonths(datum_keuring, -24)     // Get date 24 months before Datum keuring
If (vBirthdate >= vFirst) And (vBirthdate <= vLast)
{
@MSG("Good Dog!")
}
Else
{
WriteLn("Bad Dog!")
}
}

else if klasse = 4 then
{
vBirthdate = GEB_D
vFirst = GetDateByMonths(datum_keuring, -9)    // Get date 9 months before Datum keuring
vLast = GetDateByMonths(datum_keuring, -18)     // Get date 18 months before Datum keuring
If (vBirthdate >= vFirst) And (vBirthdate <= vLast)
{
@MSG("Good Dog!")
}
Else
{
WriteLn("Bad Dog!")
}
}

else if klasse = 5 then
{
vBirthdate = GEB_D
vFirst = GetDateByMonths(datum_keuring, -15)    // Get date 15 months before Datum keuring
vLast = GetDateByMonths(datum_keuring, -9999)     // Get date 9999 months before Datum keuring
If (vBirthdate >= vFirst) And (vBirthdate <= vLast)
{
@MSG("Good Dog!")
}
Else
{
WriteLn("Bad Dog!")
}
}

else if klasse = 6 then
{
vBirthdate = GEB_D
vFirst = GetDateByMonths(datum_keuring, -15)    // Get date 15 months before Datum keuring
vLast = GetDateByMonths(datum_keuring, -9999)     // Get date 9999 months before Datum keuring
If (vBirthdate >= vFirst) And (vBirthdate <= vLast)
{
@MSG("Good Dog!")
}
Else
{
WriteLn("Bad Dog!")
}
}

else if klasse = 7 then
{
vBirthdate = GEB_D
vFirst = GetDateByMonths(datum_keuring, -144)    // Get date six months before Datum keuring
vLast = GetDateByMonths(datum_keuring, -9999)     // Get date nine months before Datum keuring
If (vBirthdate >= vFirst) And (vBirthdate <= vLast)
{
@MSG("Good Dog!")
}
Else
{
WriteLn("Bad Dog!")
}
}

Leeftijd0 = GetDateByMonths(Datum_keuring, 0)  // Get date 0 months before Datum keuring

goto ring[/tr]

What everi try i always get the message "Bad Dog"

What i have do wrong in de code?

W
  
Back to top
IP Logged
 
Ray the Reaper
Global Moderator
Members
Lantica Support
*****
Offline


The One & The Only

Posts: 2480
Joined: Aug 20th, 2003
Re: Problem with checking age.
Reply #1 - Jul 10th, 2008 at 1:30pm
Print Post Print Post  
If datum_keuring is 2008/07/10 and Klasse is 1, vFirst would be 2008/01/10 and vLast would be 2007/10/10. There is no Date in the world that is greater than 2008/01/10 And less than 2007/10/10. If you want to find if the date is between those two switch vFirst and vLast, so that vFirst would be 2007/10/10 and vLast would be 2008/01/10.

-Ray
  

Raymond Yoxall Consulting
ray.yoxall@gmail.com
ryoxall@lantica.com
Sesame Applications, Design and Support
Back to top
IP Logged
 
alex_smits
Member
*
Offline


No personal text

Posts: 27
Location: Arnhem, The Netherlands
Joined: Dec 15th, 2003
Re: Problem with checking age.
Reply #2 - Oct 26th, 2008 at 2:52pm
Print Post Print Post  
This function don't work with date's for example 2008/12/31 en calculating -10 months, because 2008/02/31 do not exist the function result 0000/00/00 how can i solve this problem.
  
Back to top
IP Logged
 
Ray the Reaper
Global Moderator
Members
Lantica Support
*****
Offline


The One & The Only

Posts: 2480
Joined: Aug 20th, 2003
Re: Problem with checking age.
Reply #3 - Oct 27th, 2008 at 3:48pm
Print Post Print Post  
Hello Alex,

Could you give me some more details on the problem that you are having?

-Ray
  

Raymond Yoxall Consulting
ray.yoxall@gmail.com
ryoxall@lantica.com
Sesame Applications, Design and Support
Back to top
IP Logged
 
alex_smits
Member
*
Offline


No personal text

Posts: 27
Location: Arnhem, The Netherlands
Joined: Dec 15th, 2003
Re: Problem with checking age.
Reply #4 - Oct 27th, 2008 at 10:39pm
Print Post Print Post  
The problem is calculating the age in months.

I get this code after asking my question on this forum.
My Questions was:
I using Sesame for the administration for a International Dogshow, 
We have several classes with must be checked between the day of the show and the birthday of the dog. 

For example we have puppy class for dog from 6-9 months and the young dogs class for dogs from 15-24 months on the day of the show. 

A dog entered in the puppy class for 10 may 2008 must be born before 11 november 2007 and after 
10 august 2007. 

Can some one give me an example how i can let sesame check if the dog is entered in the correct class. 

In Q&A is checked this on the class en manualy calculed the date's between the dog must be born to enter in to that class, Every year again.  Is there a posibility to calculated the age in months from the day of the show.

Our Dosgshow by Whitsun in 2009 falls on 30, 31 may and 1 july.

When i calculating the age with this code i get date's 0000/00/00 return because not every month have 31 days.
  
Back to top
IP Logged
 
Ray the Reaper
Global Moderator
Members
Lantica Support
*****
Offline


The One & The Only

Posts: 2480
Joined: Aug 20th, 2003
Re: Problem with checking age.
Reply #5 - Oct 31st, 2008 at 5:09pm
Print Post Print Post  
What code do you currently have?

Would it be possible to send the DB and DAT files into support@lantica.com so that we can work on them and see where the problem is at?

-Ray
  

Raymond Yoxall Consulting
ray.yoxall@gmail.com
ryoxall@lantica.com
Sesame Applications, Design and Support
Back to top
IP Logged
 
alex_smits
Member
*
Offline


No personal text

Posts: 27
Location: Arnhem, The Netherlands
Joined: Dec 15th, 2003
Re: Problem with checking age.
Reply #6 - Nov 1st, 2008 at 2:47pm
Print Post Print Post  
Ray,

I have send the file to support

Alex
  
Back to top
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: Problem with checking age.
Reply #7 - Nov 3rd, 2008 at 1:21am
Print Post Print Post  
Alex,

Say today is Dec 31, 2008.

What is the correct answer to today - 10 months? There is no Feb 31, so what date would you want back in that case?
  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged
 
alex_smits
Member
*
Offline


No personal text

Posts: 27
Location: Arnhem, The Netherlands
Joined: Dec 15th, 2003
Re: Problem with checking age.
Reply #8 - Nov 3rd, 2008 at 7:16pm
Print Post Print Post  
It's must return the last possible date of the month, so for your example, Feb, 29

the calculating on this site works fine for me
http://www.timeanddate.com/date/dateadd.html

  
Back to top
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: Problem with checking age.
Reply #9 - Nov 3rd, 2008 at 7:19pm
Print Post Print Post  
alex_smits wrote on Nov 3rd, 2008 at 7:16pm:
It's must return the last possible date of the month, so for your example, Feb, 29


Thanks, Alex. I'll write you a code snippet in a little bit to do this. I started to do it yesterday and realized I wasn't sure what answer you would want. Month math is a pain because they vary in length.  Smiley

I'm in the middle of something, but I'll get to this shortly.
  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: Problem with checking age.
Reply #10 - Nov 3rd, 2008 at 7:38pm
Print Post Print Post  
Alex,

Try replacing your GLOBAL CODE with the code below. It knows the last day valid for each month including a leap year check.

Code
Select All
stat sLastDays as Array[12] of Int

// Returns whether a given year is a leap year
FUNCTION IsLeapYear(vTestYear as Int)
var vRet as Int

	vRet = 0
	If ((@Mod(vTestYear, 4) = 0) And (@Mod(vTestYear, 100) <> 0)) Or (@Mod(vTestYear, 400) = 0)
	{
		vRet = 1
	}

	Return vRet

END FUNCTION

// Returns a date that is vMonths from vStartDate  
// Use negative vMonths to get a date before vStartDate
FUNCTION GetDateByMonths(vStartDate as Date, vMonths as Int) as Date
var vRet as Date
var vYear as Int
var vDay as Int
var vMonth as Int
var vOffset as Int  
var vDir as Int // Add (1) or subtract (-1) months
var vLastDay as Int

	vRet = vStartDate
	vDir = 1
	vOffset = vMonths

	// Split date into pieces
	vYear = @Int(@Year(vStartDate))
	vDay = @DOM(vStartDate)
	vMonth = @Month(vStartDate)

	// Work with a positive number of months
	If vOffset < 0  
	{
		vDir = -1
		vOffset = @Abs(vOffset)
	}

	// Adjust months
	If vDir = 1
	{
		vMonth = vMonth + vOffset

		// Adjust for multiple years
		While vMonth > 12
		{
			vYear = vYear + 1
			vMonth = vMonth - 12
		}
	}
	Else
	{
		vMonth = vMonth - vOffset

		// Adjust for multiple years
		While vMonth < 1
		{
			vYear = vYear - 1
			vMonth = vMonth + 12
		}
	}

	// Adjust for month length
	vLastDay = sLastDays[vMonth]
	If vMonth = 2
	{
		vLastDay = vLastDay + IsLeapYear(vYear)
	}
	If vDay > vLastDay
	{
		vDay = vLastDay
	}

	// Assemble new date
	vRet = @Str(vYear) + "/" + @Text(2 - @Len(@Str(vMonth)), "0") + @Str(vMonth) + "/" + @Text(2 - @Len(@Str(vDay)), "0") + @Str(vDay)  
	Return vRet  

END FUNCTION  

	// Populate array with the last valid day of each month
	sLastDays[1] = 31
	sLastDays[2] = 28
	sLastDays[3] = 31
	sLastDays[4] = 30
	sLastDays[5] = 31
	sLastDays[6] = 30
	sLastDays[7] = 31
	sLastDays[8] = 31
	sLastDays[9] = 30
	sLastDays[10] = 31
	sLastDays[11] = 30
	sLastDays[12] = 31

 

  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged