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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Months between two dates?

Status
Not open for further replies.

meinhunna

Programmer
Jul 31, 2004
118
0
0
AU

how can i find number of months between two dates (inclusive)

for eg

01/01/2004 and 20/12/2004 ==> 12

i have tried using DateDiff function but it results in 11

thanks



















 
So are you intending to ignore the day part of the date completely?

For example, should 15/1/04 to 13/12/04 result in 12?

"Life is full of learning, and then there is wisdom"
 
Just use the Month() function:

Month("15/1/04") will return 1
Month("13/12/04") will return 12

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'

for steam enthusiasts
 
The challenge with the Month() function is that if the dates are in different years, then the logic may have to compensate for that fact.

However AEtherson's solution has that fact built in.

"Life is full of learning, and then there is wisdom"
 
Of course, if we consider 31 Jan 2005 and 1 Feb 2005, most of these solutions run in to problems.

A keyword search in this forum will find a (quite large)number of previous threads on this subject, many of which go into the details of where you can be caught out. Probably worth a search and a read.
 
What if you break it down by Month Day And Year ... and calculate it like this...

Code:
Date1 = "01/01/2004"
Date2 = "21/12/2005"
M = Split(Date1,"/")(1) - Split(Date2,"/")(1)
D = Split(Date1,"/")(0) - Split(Date2,"/")(0)
Y = Split(Date1,"/")(2) - Split(Date2,"/")(2)
Y = Y * 12
D = D / Abs(D)
M = Abs(M + D + Y)

Any reason that would not work?

Supporting Data, and expanation...
Code:
  01  01  2004
 -21 -12 -2004
--------------
 -20 -11  0000
Or
Code:
  21  12  2004
 -01 -01 -2004
--------------
  20  11  0000

31 Jan 2005 and 1 Feb 2005
Code:
  31  01  2005
 -01 -02 -2005
--------------
  30 -01  0000
Or
Code:
  01  02  2005
 -31 -01 -2005
--------------
 -30  01  0000

**************
Code:
Date1 = "31/01/2005"
Date2 = "01/02/2005"
M = Split(Date1,"/")(1) - Split(Date2,"/")(1)   '= -1
D = Split(Date1,"/")(0) - Split(Date2,"/")(0)   '= 30
Y = Split(Date1,"/")(2) - Split(Date2,"/")(2)   '= 2004 - 2004 ... 0
Y = Y * 12                                      '= 0 * 12 ... 0
D = D / Abs(D)                                  '= 30 / 30 ... 1
M = Abs(M + D + Y)                              '= Abs(-1 + 1 + 0) ... 0

-----Or-------

Code:
Date1 = "01/01/2004"
Date2 = "21/12/2004"
M = Split(Date1,"/")(1) - Split(Date2,"/")(1)   '= 01 - 12 ... -11
D = Split(Date1,"/")(0) - Split(Date2,"/")(0)   '= 01 - 21 ... -20
Y = Split(Date1,"/")(2) - Split(Date2,"/")(2)   '= 2004 - 2004 ... 0
Y = Y * 12                                      '= 0 * 12 ... 0
D = D / Abs(D)                                  '= -20 / 20 ... -1
M = Abs(M + D + Y)                              '= Abs(-11 + -1 + 0) ... 12

-----Or-------

Code:
Date1 = "01/01/2004"
Date2 = "21/12/2005"
M = Split(Date1,"/")(1) - Split(Date2,"/")(1)   '= 01 - 12 ... -11
D = Split(Date1,"/")(0) - Split(Date2,"/")(0)   '= 01 - 21 ... -20
Y = Split(Date1,"/")(2) - Split(Date2,"/")(2)   '= 2004 - 2005 ... -1
Y = Y * 12                                      '= -1 * 12 ... -12
D = D / Abs(D)                                  '= -20 / 20 ... -1
M = Abs(M + D + Y)                              '= Abs(-11 + -1 + -12) ... 24

Is there an instance where this would not work?

Have Fun, Be Young... Code BASIC
-Josh

cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top