Page 1 of 2

HMG Program to Calculate your Age!

Posted: Thu Jul 31, 2008 7:17 am
by Rathinagiri
This is a small utility to show the power and simplicity of HMG.

You give your date of birth and today's date. Find your age in year(s),month(s) and day(s)!

Image

Code: Select all

//starting of programming code

# include "minigui.ch"

function main
define window age at 0,0 width 250 height 200 title "Find your age" main
define label bdaylabel
   row 10
   col 10
   value "Your Birthday :"
end label
define datepicker bdate
   row 10
   col 120
   value date()
end datepicker
define label todaylabel
   row 40
   col 10
   value "Today is :"
end label
define datepicker today
   row 40
   col 120
   value date()
end datepicker
define label age1
    row 70
    col 10
	width 230
	height 50
end label
define button find
   row 120
   col 100
   width 60
   caption "Find Age"
    action findage()
end button
end window
age.center
age.activate
return nil


function findage
if age.today.value < age.bdate.value
   msgstop("Today should be greater than the birthday!","Find Age")
   age.age1.value := "Invalid dates"
   return nil
endif
years := 0
months := 0
days := 0

years := year(age.today.value) - year(age.bdate.value)
months := month(age.today.value) - month(age.bdate.value) 
if months < 0
   years := years - 1
   months := months + 12
endif
days := day(age.today.value) - day(age.bdate.value)
if days < 0
   months := months - 1
   days := days + 30
   if months < 0
      years := years - 1
      months := months + 12
   endif
endif
age.age1.value := "Age is "+str(years)+" Year(s),"+str(months)+" Month(s),"+str(days)+" Day(s)."
return nil

//end of programming code
How to create age.exe?

1.Copy this code into a new age.prg file.
2. Just run "c:\hmg\batch\compile age" in a command window from the directory where the age.prg is placed.
3. You can get the executable file age.exe.
4. That's all!

Re: HMG Program to Calculate your Age!

Posted: Thu Jul 31, 2008 6:49 pm
by Roberto Lopez
rathinagiri wrote:This is a small utility to show the power and simplicity of HMG.

You give your date of birth and today's date. Find your age in year(s),month(s) and day(s)!
Oh my God.... I have 46 years, 2 moths and 16 days!!!!

You must add and advisory about the program depressive effects in persons older than 40 years :)

Regards,

Roberto.

Re: HMG Program to Calculate your Age!

Posted: Thu Jul 31, 2008 6:54 pm
by esgici
...and person (like me) more older never will touch this program :)

regards

esgici

Re: HMG Program to Calculate your Age!

Posted: Fri Aug 01, 2008 3:31 am
by Rathinagiri
Ha Ha. :)

Thank you.

Re: HMG Program to Calculate your Age!

Posted: Sat Aug 02, 2008 4:35 pm
by esgici
Does we haven't a function for calculating difference between two date in date type?

If so we need it. Such as

Dif2Date( <dDate1>, <dDate2> ) => dDateDifference

IMHO it have a Harbour core library function.

Regards

esgici

Re: HMG Program to Calculate your Age!

Posted: Sat Aug 02, 2008 5:27 pm
by Rathinagiri
Nice idea esgici.

Re: HMG Program to Calculate your Age!

Posted: Sun Aug 03, 2008 1:47 pm
by Pete-WG
esgici wrote:Does we haven't a function for calculating difference between two date in date type?

If so we need it. Such as

Dif2Date( <dDate1>, <dDate2> ) => dDateDifference

IMHO it have a Harbour core library function.

Regards

esgici
Hi esgici,

Do you mean something like: Dif2Date( 01/01/2008, 03/08/2008) => dd/mm/yyyy ...
or what?
I ask because i cannot understand how the calculation of "difference between two date" could be resulted to a value of date type.
Normally, the calculation result of difference between two dates is a numeric value denoting days. Of course this numeric value, could , with some way, to converted to date, but still i cannot figure out what could be the meaning and usage of such a date. Could you clarify?

thanks,


---
Pete

Re: HMG Program to Calculate your Age!

Posted: Sun Aug 03, 2008 2:20 pm
by esgici
Hi Pete
Do you mean something like: Dif2Date( 01/01/2008, 03/08/2008) => dd/mm/yyyy ... or what?
Yes, I means exactly this.
... how the calculation of "difference between two date" could be resulted to a value of date type.
...
... i cannot figure out what could be the meaning and usage of such a date.
Second behalf of Rathinagiri's program is findage() function and dedicated to this purpose. If we have such function, other than this long calculations, we can obtain difference in only single line. Such as:

dAge := Dif2Date( age.today.value, age.bdate.value )

age.age1.value := "Age is "+str(YEAR(dAge))+" Year(s),"+str(MONTH(dAge))+" Month(s),"+str(DAY(dAge))+" Day(s)."
Normally, the calculation result of difference between two dates is a numeric value denoting days.
Yes, because of this we need such function.

The problem is: Although a date difference may have zero for year, month and/or day; by definition, a date type value can not have any partial value out of range. f.e. mounth must be 1..12, day must be 1..31, etc.

Because of this rule, we haven't such diff function. My whish is a "guru" will solve it :)

Regards

Re: HMG Program to Calculate your Age!

Posted: Sun Aug 03, 2008 2:26 pm
by Pete-WG
rathinagiri wrote:This is a small utility to show the power and simplicity of HMG.
You give your date of birth and today's date. Find your age in year(s),month(s) and day(s)!
<snip>
...
4. That's all!

Rathinag,

Please do not get me wrong, but for sure, your nice "Age calculating" proggy is out of my interest. Believe me, i can think better things to ruin my day.
However, you could attract a fair number of users if you made one or two modifications.
For example, how about, having the program ask the user good questions like:
- "Hi dude, no matter how old are you, just tell how much do you feel?"
or
- "tell me what you were doing last night, to tell you how old are you.."


Just think about... ;-)


---
Pete

Re: HMG Program to Calculate your Age!

Posted: Sun Aug 03, 2008 3:45 pm
by Pete-WG
esgici wrote:Hi Pete
Do you mean something like: Dif2Date( 01/01/2008, 03/08/2008) => dd/mm/yyyy ... or what?
Yes, I means exactly this.

Because of this rule, we haven't such diff function. My whish is a "guru" will solve it :)

Regards
Well, i am definitely NOT a "guru" but here is a "quick and dirty" version of your Dif2Date() function.

I very suspect that It has some problem(s), though... (particularly regarding years). Please make some tests, to see what i mean.

Code: Select all

Proc Main()
local dStartDate, dEndDate, dDiff
set date british
dStartDate := ctod("21/06/1957")
dEndDate   := date()

dDiff := Dif2Date( dStartDate, dEndDate )

? dDiff

? str( year(dDiff)) + " œyears, " + ;
  str(month(dDiff)) + " months, " + ;
  str(day(dDiff)) + " ždays"



#define nTrim(n)  LTrim( Str( n ) )
*****************************************
function Dif2Date( dStartDate, dEndDate )
*****************************************
local nYears, nMonths, nDays, nLipyears

nDays := dEndDate - dStartDate
nYears := int( nDays / 365 )
nLipyears := int( nYears / 4 )
nDays := nDays % 365
nMonths := int(  nDays / 30 ) 
nDays := int( nDays % 30 ) - nLipYears

/* ? nYears, nMonths, nDays */

// return CTOD( alltrim(str(nDays))+"/"+alltrim(str(nMonths))+"/"+alltrim(str(nYears)))
return CTOD( nTrim(nDays) + "/" + nTrim(nMonths) + "/" + nTrim(nYears) )
Quickly tested on harbour console mode, not MiniGUI..