HMG Program to Calculate your Age!

HMG Samples and Enhancements

Moderator: Rathinagiri

User avatar
Rathinagiri
Posts: 5471
Joined: Tue Jul 29, 2008 6:30 pm
DBs Used: MariaDB, SQLite, SQLCipher and MySQL
Location: Sivakasi, India
Contact:

HMG Program to Calculate your Age!

Post 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!
East or West HMG is the Best.
South or North HMG is worth.
...the possibilities are endless.
User avatar
Roberto Lopez
HMG Founder
Posts: 4004
Joined: Wed Jul 30, 2008 6:43 pm

Re: HMG Program to Calculate your Age!

Post 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.
Regards/Saludos,

Roberto


(Veritas Filia Temporis)
User avatar
esgici
Posts: 4543
Joined: Wed Jul 30, 2008 9:17 pm
DBs Used: DBF
Location: iskenderun / Turkiye
Contact:

Re: HMG Program to Calculate your Age!

Post by esgici »

...and person (like me) more older never will touch this program :)

regards

esgici
Viva INTERNATIONAL HMG :D
User avatar
Rathinagiri
Posts: 5471
Joined: Tue Jul 29, 2008 6:30 pm
DBs Used: MariaDB, SQLite, SQLCipher and MySQL
Location: Sivakasi, India
Contact:

Re: HMG Program to Calculate your Age!

Post by Rathinagiri »

Ha Ha. :)

Thank you.
East or West HMG is the Best.
South or North HMG is worth.
...the possibilities are endless.
User avatar
esgici
Posts: 4543
Joined: Wed Jul 30, 2008 9:17 pm
DBs Used: DBF
Location: iskenderun / Turkiye
Contact:

Re: HMG Program to Calculate your Age!

Post 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
Viva INTERNATIONAL HMG :D
User avatar
Rathinagiri
Posts: 5471
Joined: Tue Jul 29, 2008 6:30 pm
DBs Used: MariaDB, SQLite, SQLCipher and MySQL
Location: Sivakasi, India
Contact:

Re: HMG Program to Calculate your Age!

Post by Rathinagiri »

Nice idea esgici.
East or West HMG is the Best.
South or North HMG is worth.
...the possibilities are endless.
User avatar
Pete-WG
Posts: 14
Joined: Sat Aug 02, 2008 6:28 am

Re: HMG Program to Calculate your Age!

Post 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
User avatar
esgici
Posts: 4543
Joined: Wed Jul 30, 2008 9:17 pm
DBs Used: DBF
Location: iskenderun / Turkiye
Contact:

Re: HMG Program to Calculate your Age!

Post 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
Viva INTERNATIONAL HMG :D
User avatar
Pete-WG
Posts: 14
Joined: Sat Aug 02, 2008 6:28 am

Re: HMG Program to Calculate your Age!

Post 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
User avatar
Pete-WG
Posts: 14
Joined: Sat Aug 02, 2008 6:28 am

Re: HMG Program to Calculate your Age!

Post 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..
Post Reply