New call-to-action
Results 1 to 7 of 7

Thread: Calculate Age in a Dialog

  1. #1
    Member
    Real Name
    John Berry
    Join Date
    Nov 2007
    Location
    Maryland, USA
    Posts
    660

    Default Calculate Age in a Dialog

    Probably a simple answer to this but I can't find it. How do I calculate someone's age in a dialog? I have a field called DOB as a date field. I have a text box (textbox1) next to it where I want to just show the user the person's age, this field does not need to be saved I just want to display it to the user. If I go to the Calculated Field Expression of textbox1 I see that I cannot use the AGE() function nor the DATE() function so I'm now lost on how to calculate the field. A simple (Date()-DOB)/365.25) would be sufficient for my purposes but it looks like it's not allowed. Any ideas?

    Thanks

    -John

  2. #2
    "Certified" Alphaholic Keith Hubert's Avatar
    Real Name
    Keith Hubert
    Join Date
    Jul 2000
    Location
    London, UK
    Posts
    6,930

    Default Re: Calculate Age in a Dialog

    Have your tried age(field with DOB)?
    Regards
    Keith Hubert
    Alpha Guild Member
    London.
    KHDB Management Systems
    Skype = keith.hubert


    For your day-to-day Needs, you Need an Alpha Database!

  3. #3
    Member
    Real Name
    John Berry
    Join Date
    Nov 2007
    Location
    Maryland, USA
    Posts
    660

    Default Re: Calculate Age in a Dialog

    Hi Keith

    Yup. Using the AGE() function is not allowed and neither is DATE(). I think what I need to do is an Ajax Callback on the OnChange of the DOB field.

    If anyone knows a way to do it in the Calculated Field Expression of a textbox I'd be interested to know.

    Thanks.

  4. #4
    Member
    Real Name
    John Berry
    Join Date
    Nov 2007
    Location
    Maryland, USA
    Posts
    660

    Default Re: Calculate Age in a Dialog

    I got this to work by using an Ajax Callback but now I have another problem that might be a bug so if someone can verify this then I will report it.

    1) Create a textbox of type Date (for holding a date of birth). Let's call it DOB
    2) Create a textbox (for holding a calculated field). Let's call it MyField
    3) Create an Ajax Call back on the OnChange of the DOB field to stuff something into MyField. It can be anything, Int((DATE()-DOB)/365.25) for example
    4) Enter something into DOB and move off the field.

    All seems to work ok. The MyField will be stuffed with whatever you have in your Ajax Callback, however...

    5) For field DOB, add a Date/Time Picker.
    6) Enter something into DOB and move off the field.

    The MyField remains empty, it appears the OnChange of the DOB field does not fire once you add the Date Picker to it.

    Can someone verify that?

    Thanks

    -John

  5. #5
    Member
    Real Name
    Jim Coltz
    Join Date
    Oct 2005
    Location
    Western Pennsylvania
    Posts
    600

    Default Re: Calculate Age in a Dialog

    Quote Originally Posted by Jberry View Post
    The MyField remains empty, it appears the OnChange of the DOB field does not fire once you add the Date Picker to it.

    Can someone verify that?

    Thanks

    -John
    Not all control types have all the events. The onchange doesn't fire when you use a date picker. Using an ajax callback is a little overkill for what you want. In your textbox1 field, in the client side properties, Calculated field expression insert getAge(DOB) Then, in the Code, Javascipt functions section paste this code in
    Code:
    function getAge(dateString) {
        if(dateString != "" && dateString.length >7 ){
    	    var today = new Date();
    	    var birthDate = new Date(dateString);
    	    var age = today.getFullYear() - birthDate.getFullYear();
    	    var m = today.getMonth() - birthDate.getMonth();
    	    if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
    	        age--;
    	    }
    	    return age;
    	 }else{
    	 	return "";
    	 }   
    }
    This will do all the calculation on the browser and will work with a date picker as well. I've attached a simple dialog component as an example. You can drop it into your web project folder to see how it works. If you type into the DOB control you'll need to tab out to see the results.
    Attached Files Attached Files
    Jim Coltz
    Alpha Custom Database Solutions, LLC
    A5CustomSolutions.com
    jimcoltz@a5customsolutions.com

  6. #6
    Member
    Real Name
    John Berry
    Join Date
    Nov 2007
    Location
    Maryland, USA
    Posts
    660

    Default Re: Calculate Age in a Dialog

    Hi Jim,

    Thanks for that. Yes an Ajax callback is probably overkill but as I don't know JS I hav no choice :) I'll try this and let you know how it goes.

    Thanks again.

  7. #7
    Member
    Real Name
    John Berry
    Join Date
    Nov 2007
    Location
    Maryland, USA
    Posts
    660

    Default Re: Calculate Age in a Dialog

    Jim, thanks. Works like a champ.

Similar Threads

  1. Age in months of birth date to age in futrue
    By jdoyle in forum Alpha Five Version 10 - Desktop Applications
    Replies: 14
    Last Post: 01-28-2010, 04:46 PM
  2. calculate age from 2 separate tables?
    By dlazenby in forum Alpha Five Version 9 - Desktop Applications
    Replies: 4
    Last Post: 09-15-2009, 08:14 PM
  3. Compute Age
    By Jberry in forum Application Server Version 9 - Web/Browser Applications
    Replies: 2
    Last Post: 07-01-2009, 12:35 AM
  4. Calculate age
    By Edgar Lewis in forum Alpha Five Version 4
    Replies: 2
    Last Post: 05-06-2001, 05:01 AM
  5. Calculate age
    By Joseph Mann in forum Alpha Five Version 4
    Replies: 16
    Last Post: 03-13-2001, 04:56 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •