INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Routine to give difference between two dates as words (years, months, weeks and days)

Routine to give difference between two dates as words (years, months, weeks and days)

Routine to give difference between two dates as words (years, months, weeks and days)

(OP)
I needed to express the difference between two dates as a string with years, months, weeks and days being split out and found very little to help do this in VFP.

The trick - it seems to me - is to calculate the number of months first, using GoMonth() iteratively. Years are always 12 months, so they can be split off.
Then 'rebase' the earlier date to the whole months position, and calculate the number of days remaining - then the weeks can be split off as these are always 7 days.
This can then be used to work out the remaining days.

Might not be any use to anyone but me... but here it is

CODE

FUNCTION DATEDIFFASWORDS
	PARAMETERS m.DATE1,m.DATE2
	PRIVATE m.DATE1,m.DATE2, 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
	m.MONTHS = 0
	IF GOMONTH(m.DATE1, 1) < m.DATE2
		DO WHILE GOMONTH(m.DATE1, m.MONTHS) < m.DATE2
			m.MONTHS = m.MONTHS + 1
		ENDDO
		m.MONTHS = m.MONTHS-1
	ENDIF

	m.DATE1 = GOMONTH(m.DATE1,m.MONTHS)
	m.DAYS = m.DATE2-m.DATE1
	m.WEEKS = INT(m.DAYS/7)
	m.DAYS = m.DAYS - (m.WEEKS * 7)
	m.YEARS = INT(m.MONTHS/12)
	m.MONTHS = m.MONTHS - (m.YEARS * 12)
	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 Smileing

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.

RE: Routine to give difference between two dates as words (years, months, weeks and days)

(OP)
I have created a FAQ for this, and improved the code very slightly - waiting for number...
Regards

Griff
Keep Smileing

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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close