 |
|
 |
victor77 Level: Trainee
 Registered: 12-01-2008 Posts: 1
|
calculating the difference between 2 dates
Greetings!!!
I need help in calculating the difference between 2 dates (the result should be like "1 year, 2 months and 15 days"). I am building an application for my team to show their tenure in the organization (LIVE TENURE FROM THE DATE OF HIRE TILL NOW). I have the formula in EXCEL but I am not able to get it converted into Visual Basic (I am only a beginner).
I am using Visual Basic 6.0 along with ADO connection.
The formula in Excel is "=YEAR(TODAY())-YEAR(E337)-IF(OR(MONTH(TODAY())<MONTH(E337),AND(MONTH(TODAY())=MONTH(E337),DAY(TODAY())<DAY(E337))),1,0)&" years, "&MONTH(TODAY())-MONTH(E337)+IF(AND(MONTH(TODAY())<=MONTH(E337),DAY(TODAY())<DAY(E337)),11,IF(AND(MONTH(TODAY())<MONTH(E337),DAY(TODAY())>=DAY(E337)),12,IF(AND(MONTH(TODAY())>MONTH(E337),DAY(TODAY())<DAY(E337)),-1)))&" months, "&TODAY()-DATE(YEAR(TODAY()),MONTH(TODAY())-IF(DAY(TODAY())<DAY(E337),1,0),DAY(E337))&" days""
I need this to be completed very soon and I appreciate any help that I can get on this question.
Looking forward to hear from you,
Thank You,
Venugopal
|
|
12-01-2008 at 09:48 PM |
|
|
JLRodgers Level: Moderator
 Registered: 04-04-2002 Posts: 1627
|
Re: calculating the difference between 2 dates
VB has a "datediff" function that gives you the difference between dates in whatever interval you're wanting (well, seconds, minutes, days, etc -- but not all at once I don't believe [i.e. 5 days, but not 5 days 4 hours 3 minutes 25 seconds]).
If in VB6, and you type in "datediff(" you'll be prompted with items to choose from.
from microsoft:
http://msdn2.microsoft.com/en-us/library/b5xbyt6f.aspx
(I don't have VB6 installed on the primary computer anymore, since it's not compatible -- otherwise I could provide examples).
but it'd be like
DateDiff("y", startdate, now) ' the number of years
' For months, days would require more calculations
totalmonths = DateDiff("m", startdate, now)
totalyears = int(totalmonths/12)
totalmonths = totalmonths - totalyears*12
' so the person has worked for "totalyears, totalmonths"
|
____________________________
Everywhere's Local (classifieds, job postings, & more for everycity in the world - user entered)
|
|
12-01-2008 at 10:59 PM |
|
|
|
|
 |
 |