Normal Topic calculating the age in months (Read 396 times)
alex_smits
Member
*
Offline


No personal text

Posts: 27
Location: Arnhem, The Netherlands
Joined: Dec 15th, 2003
calculating the age in months
Nov 25th, 2007 at 3:11pm
Print Post Print Post  
Hello,

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. 

Thank you.

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


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: calculating the age in months
Reply #1 - Nov 26th, 2007 at 3:00pm
Print Post Print Post  
Alex,

Here is a function that returns a date a specified number of months from the requested date.

Code
Select All
// 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 



By placing this function in GLOBAL CODE, you might use it as follows for your needs. This assumes the dog's birth date is in an element named Dog_Birthdate. The example below covers the six-nine moths old case.

Code
Select All
var vBirthdate as Date
var vFirst as Date
var vLast as Date

	vBirthdate = Dog_Birthdate
	vFirst = GetDateByMonths(@ServerDate(), -6)    // Get date six months before today
	vLast = GetDateByMonths(@ServerDate(), -9)     // Get date nine months before today
	If (vBirthdate >= vFirst) And (vBirthdate <= vLast)
	{
		WriteLn("Good Dog!")
	}
	Else
	{
		WriteLn("Bad Dog!")
	}

 

  

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