A customer wants to have a countdown on his reports to a series of target dates - 2 years, 3 months, 5 days to whatever.
Sounds simple enough doesn't it?
The snag is, there are only two date based units that are actually fixed, days and weeks. Months and years are of variable length.
** edit ** To be fair, years are both fixed and variable - always twelve months, not always 365 days
So below is the code I'm using - anyone got something better to add?
Regards
Griff
Keep [Smile]ing
Sounds simple enough doesn't it?
The snag is, there are only two date based units that are actually fixed, days and weeks. Months and years are of variable length.
** edit ** To be fair, years are both fixed and variable - always twelve months, not always 365 days
So below is the code I'm using - anyone got something better to add?
Code:
FUNCTION DATEDIFFASWORDS
LPARAMETERS m.DATE1,m.DATE2
LOCAL m.YEARS,m.MONTHS,m.WEEKS,m.DAYS,m.TEMP,m.STRING
m.YEARS = 0
m.MONTHS = 0
m.WEEKS =0
m.DAYS = 0
** swap dates if needs be m.date2 should be the greater
IF m.DATE1 > m.DATE2
m.TEMP = m.DATE1
m.DATE1 = m.DATE2
m.DATE2 = m.TEMP
ENDIF
** Determine No. Months beteen dates
m.MONTHS = 0
IF GOMONTH(m.DATE1, 1) <= m.DATE2
**m.MONTHS = (YEAR(m.DATE2)*12+MONTH(m.DATE2))-(YEAR(m.DATE1)*12+MONTH(m.DATE1))
** Edit line changed after posting
m.MONTHS = (YEAR(m.DATE2+1)*12+MONTH(m.DATE2+1))-(YEAR(m.DATE1)*12+MONTH(m.DATE1))-1
ENDIF
** advance earlier date on by that many months
m.DATE1 = GOMONTH(m.DATE1,m.MONTHS)
** determine the number of days left
m.DAYS = m.DATE2-m.DATE1
** turn that into whole weeks
m.WEEKS = INT(m.DAYS/7)
** determine the number of days left if you take away that many weeks
m.DAYS = m.DAYS - (m.WEEKS * 7)
** use the months to calculate the number of whole years
m.YEARS = INT(m.MONTHS/12)
m.MONTHS = m.MONTHS - (m.YEARS * 12)
** assemble your answer in words.
m.STRING = ""
IF m.YEARS > 0
m.STRING = ALLTRIM(STR(m.YEARS)+ " Year"+IIF(m.YEARS>1,"s",""))
ENDIF
IF m.MONTHS > 0
m.STRING = ALLTRIM( m.STRING +" "+ALLTRIM(STR(m.MONTHS))+" Month"+IIF(m.MONTHS>1,"s",""))
ENDIF
IF m.WEEKS > 0
m.STRING = ALLTRIM( m.STRING +" "+ALLTRIM(STR(m.WEEKS))+" Week"+IIF(m.WEEKS>1,"s",""))
ENDIF
IF m.DAYS > 0
m.STRING = ALLTRIM( m.STRING +" "+ALLTRIM(STR(m.DAYS))+" Day"+IIF(m.DAYS>1,"s",""))
ENDIF
RETURN(m.STRING)
Regards
Griff
Keep [Smile]ing
There are 10 kinds of people in the world, those who understand binary and those who don't.
I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are !good for you.
There is no place like G28 X0 Y0 Z0
I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are !good for you.
There is no place like G28 X0 Y0 Z0