Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calculate Date + 3 months + Next 1st of Month? 1

Status
Not open for further replies.

buddyrich2

Technical User
Apr 12, 2006
87
US
A person is eligible for something on the first date of the month following the date that he has been with the company for 3 months. I only have a date of hire.

Can anyone help me with a calculation for this?

 
You can 'build' a First-Of-Month date from the HireDate

Code:
* --- Build First-Of-Month of HireDate ---
dHireDate = <whatever>
cMonth = PADL(ALLTRIM(STR(MONTH(dHireDate))),2,'0')
cYear = STR(YEAR(dHireDate),4)
dStartHireMonth = CTOD('01/' + cMonth + '/' + cYear)

And then use the VFP Function GOMONTH(dStartHireMonth,<whatever>) to get another date based on the first date.

If you need to do so, you can look into your VFP Help for GOMONTH()

Good Luck,
JRB-Bldr
 
I think it's better to do this kind of calculation with only the date functions and stay away from strings.

For first-of-month:

Code:
dFOM = dDate - Day(dDate) + 1

In this case, sounds like being there three months should be computed first (though I can't imagine a case in which it matters). So, the entire calculation would be:

Code:
dThreeMonths = GOMONTH(dHired, 3)
dEligible = m.dThreeMonths - DAY(m.dThreeMonths) + 1

Tamar
 
First of the month following hiredate + 3 month rather would be

dFourMonthsLater = GOMONTH(dHired, 4)
dEligible = m.dFourMonthsLater - DAY(m.dFourMonthsLater) + 1

You just need to keep three things in mind in regard to calendar calculations:

1. there is a gomonth() function you can use to compute the corresponding date shifted whole months, this includes shifting the 31st of one month to a 30t of another, as this is taken as the last day, so gomonth doesn't simply add 30 or so days for each month, it's intelligent, don't try to reinvent this.

Of course one thing follows gomont(date,12*n) is a way to implement goyear(), which you might think to rather do via date(year()+1,month(), day()), but which will not work for the exception of 29th of february.

2. If you add a nummber to a date, this menas adding days, unfortunately you can only add whole days, but...

3. ...if you add a number to a dateTIME, this adds seconds.

---

So all in all you can make calculations on dates and datetimes down to seconds precision - it will also be easy enough to go down to split seconds with an additional variable or field of course, but that need is rather seldom. The point is, you never have to struggle with extracting parts of the date, computing with them, handling carry over in the non decimal time system and then reconvert to date/time, you can stay with the type and compute within it.

Bye, Olaf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top