Alpha Video Training
Results 1 to 1 of 1

Thread: Age if years, months and days

  1. #1
    "Certified" Alphaholic
    Real Name
    Govindan Gandhi
    Join Date
    Aug 2008
    Location
    New York, NY
    Posts
    4,294

    Default Age if years, months and days

    While answering a question on the board I wrote a xbasic code to calculate the difference in two dates to display in years, months and days.
    I am presenting that code here for future use by any one interested in that.
    These codes are done in
    1. Xdialog.
    2. Function
    3. Web dialog.
    Here are the code segments.

    Xdilog method:
    Code:
    'Date Created: 17-Jan-2014 05:05:09 AM
    'Last Updated: 18-Jan-2014 09:30:18 AM
    'Created By  : Govindan
    'Updated By  : Govindan
    'Create an XDialog dialog box to prompt for parameters.
    DIM SHARED startDate as D
    DIM SHARED endDate as D
    DIM SHARED varC_result as C
    heading_string = "Please enter starting and ending dates"
    ok_button_label = "&OK"
    cancel_button_label = "&Cancel"
    varC_result = ui_dlg_box("Date Difference ",<<%dlg%
    {region}
    {text=55,1:heading_string};
    {endregion};
    {region}
    Start Date:| [%DATE;P=popup.calendar(dtoc(startDate));I=popup.calendar%.40startDate];
    End Date:| [%DATE;P=popup.calendar(dtoc(endDate));I=popup.calendar%.40endDate];
    {endregion};
    {line=1,0};
    {region}
    <*15=ok_button_label!OK> <15=cancel_button_label!CANCEL>
    {endregion};
    %dlg%,<<%code%
    if startDate > endDate then
    	msgbox("Difference","Error start date is higher than the end date")
    	end
    end if
    
    dim y1 as n
    dim y2 as n
    dim m1 as n
    dim m2 as n
    dim d1 as n
    dim d2 as n
    dim dY as n
    dim dM as n
    dim dD as n
    dim leapYr as l
    
    
    y1 = year(startDate)
    y2 = year(endDate)
    m1 = month(startDate)
    m2 = month(endDate)
    d1 = day(startDate)
    d2 = day(endDate)
    
    select
    	case mod(y2,4)>0
    		leapYr = .f.
    	case mod(y2,400)=0
    		leapYr = .t.
    	case mod(y2,100)=0
    		leapYr = .f.
    	case else
    	 	leapYr = .t.
    end select
    
    
    if ( d2-d1 < 1)
    	select 
    		case m2 = 1
    			d2 = d2+31
    		case m2 = 2
    			if leapYr 
    				d2 = d2+29
    			else	
    			d2 = d2+28
    			end if
    		case m2 = 3
    			d2 = d2+31
    		case m2 = 4
    			d2 = d2+30
    		case m2 = 5
    			d2 = d2+31
    		case m2 = 6
    			d2 = d2+30
    		case m2 = 7
    			d2 = d2+31
    		case m2 = 8
    			d2 = d2+31
    		case m2 = 9
    			d2 = d2+30
    		case m2 = 10
    			d2 = d2+31
    		case m2 = 11
    			d2 = d2+30
    		case m1 = 12
    			d2 = d2+31
    		end select	
    	m2 = m2-1
    	end if
    	if (m2 < m1)
    		y2 = y2-1
    		m2 = m2+12
    		end if
    dY = y2-y1
    dM = m2-m1
    dD = d2-d1
    msgbox("Difference","the difference is "+dy+" year(s) " +dM+" month(s) "+dD+" day(s) ")
    		
    
    %code%)

    Function method:
    Code:
    Date Created: 17-Jan-2014 06:39:07 AM
    'Last Updated: 18-Jan-2014 09:36:31 AM
    'Created By  : Govindan
    'Updated By  : Govindan
    FUNCTION age_full AS C (date_of_birth as d, endDate as d )
    	
    	if date_of_birth > endDate then
    	age_full = "Date of birth is later than the end date!"
    	else
    dim y1 as n
    dim y2 as n
    dim m1 as n
    dim m2 as n
    dim d1 as n
    dim d2 as n
    dim dY as n
    dim dM as n
    dim dD as n
    dim leapYr as l
    
    
    y1 = year(date_of_birth)
    y2 = year(endDate)
    m1 = month(date_of_birth)
    m2 = month(endDate)
    d1 = day(date_of_birth)
    d2 = day(endDate)
    
    select
    	case mod(y2,4)>0
    		leapYr = .f.
    	case mod(y2,400)=0
    		leapYr = .t.
    	case mod(y2,100)=0
    		leapYr = .f.
    	case else
    	 	leapYr = .t.
    end select
    
    
    if ( d2-d1 < 1)
    	select 
    		case m2 = 1
    			d2 = d2+31
    		case m2 = 2
    			if leapYr 
    				d2 = d2+29
    			else	
    			d2 = d2+28
    			end if
    		case m2 = 3
    			d2 = d2+31
    		case m2 = 4
    			d2 = d2+30
    		case m2 = 5
    			d2 = d2+31
    		case m2 = 6
    			d2 = d2+30
    		case m2 = 7
    			d2 = d2+31
    		case m2 = 8
    			d2 = d2+31
    		case m2 = 9
    			d2 = d2+30
    		case m2 = 10
    			d2 = d2+31
    		case m2 = 11
    			d2 = d2+30
    		case m1 = 12
    			d2 = d2+31
    		end select	
    	m2 = m2-1
    	end if
    	if (m2 < m1)
    		y2 = y2-1
    		m2 = m2+12
    		end if
    dY = y2-y1
    dM = m2-m1
    dD = d2-d1
    age_full = "Age is "+dy+" years " +dM+" months "+dD+" days "
    endi if
    END FUNCTION
    Web Dialog method:

    Web dialog will have two date fields to fill in
    and on submit this code will run.

    Code:
    function ageFull as c (e as p)
    	'debug(1)
    	dim startDate as d
    	dim endDate as d
    	startDate = e.dataSubmitted.start_date
    	endDate = e.dataSubmitted.end_date
    	if startDate > endDate
    		ageFull = "alert('The start date is later than the end date!');"
    	else
    	
    dim y1 as n
    dim y2 as n
    dim m1 as n
    dim m2 as n
    dim d1 as n
    dim d2 as n
    dim dY as n
    dim dM as n
    dim dD as n
    dim leapYr as l
    
    
    y1 = year(startDate)
    y2 = year(endDate)
    m1 = month(startDate)
    m2 = month(endDate)
    d1 = day(startDate)
    d2 = day(endDate)
    
    select
    	case mod(y2,4)>0
    		leapYr = .f.
    	case mod(y2,400)=0
    		leapYr = .t.
    	case mod(y2,100)=0
    		leapYr = .f.
    	case else
    	 	leapYr = .t.
    end select
    
    
    if ( d2-d1 < 1)
    	select 
    		case m2 = 1
    			d2 = d2+31
    		case m2 = 2
    			if leapYr 
    				d2 = d2+29
    			else	
    			d2 = d2+28
    			end if
    		case m2 = 3
    			d2 = d2+31
    		case m2 = 4
    			d2 = d2+30
    		case m2 = 5
    			d2 = d2+31
    		case m2 = 6
    			d2 = d2+30
    		case m2 = 7
    			d2 = d2+31
    		case m2 = 8
    			d2 = d2+31
    		case m2 = 9
    			d2 = d2+30
    		case m2 = 10
    			d2 = d2+31
    		case m2 = 11
    			d2 = d2+30
    		case m1 = 12
    			d2 = d2+31
    		end select	
    	m2 = m2-1
    	end if
    	if (m2 < m1)
    		y2 = y2-1
    		m2 = m2+12
    		end if
    dY = y2-y1
    dM = m2-m1
    dD = d2-d1
    ageFull = "alert('The difference is "+dy+" Years "+dM+" Months "+dD+" Day(s) "+"');"
    
    end if
    Last edited by GGandhi; 01-18-2014 at 10:57 AM.
    thanks for reading

    gandhi

    version 11 3381 - 4096
    mysql backend
    http://www.alphawebprogramming.blogspot.com
    ggandhi344@gmail.com
    Skype:ggandhi344@gmail.com
    1 914 924 5171

Similar Threads

  1. a5v10 web: Calculated grid fields for duration -(years,Months, days)
    By fineman77 in forum Alpha Five Version 10 - Desktop Applications
    Replies: 2
    Last Post: 01-20-2012, 09:16 AM
  2. Age in Years, Month, Days
    By John Turner in forum Code Archive
    Replies: 3
    Last Post: 12-04-2008, 03:05 PM
  3. Age in years and months
    By hsl9999 in forum Alpha Five Version 6
    Replies: 4
    Last Post: 10-20-2005, 10:01 PM
  4. Age in months and years
    By Edgar Lewis in forum Alpha Five Version 5
    Replies: 1
    Last Post: 02-01-2004, 09:16 PM
  5. age in months and years
    By Edgar Lewis in forum Alpha Five Version 5
    Replies: 3
    Last Post: 12-04-2002, 02:15 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
  •