Date Logic trouble

1. Senior Member
Join Date
Dec 1969
Posts
11,334

## Date Logic trouble

I&#039;m pretty burnt on this problem now, and would just like a fresh look at it.<BR><BR>I&#039;m trying to find the difference in months of two select dates (I know about datediff). Given the two dates, I have this function:<BR><BR>monthTotal += abs((month1 - month2)) + (12 * (abs(year1 - year2)));<BR><BR><BR>Sometimes it works, sometimes it doesn&#039;t... especially when the dates share the same year or are less than 12 months... am I missing something obvious here?

2. Senior Member
Join Date
Dec 1969
Posts
11,334

## Ugh

Sometimes ya see it as soon as ya post it...<BR><BR>If the dates are less than 12 months apart, this function won&#039;t work :(<BR><BR>Ah well...any ideas?

3. Senior Member
Join Date
Dec 1969
Location
Los Angeles, CA
Posts
21,192

## RE: Date Logic trouble

&#062;difference in months of two select dates <BR>***??<BR><BR>can you explain again??

4. Senior Member
Join Date
Dec 1969
Posts
96,118

## illogic? Or ill logic?

Consider:<BR><BR>Using abs((month1 - month2)) + (12 * (abs(year1 - year2)))<BR><BR><BR>March 2001 vs. February 2000<BR>becomes<BR>abs(3-2) + 12 * abs(2001-2000) --&#062;&#062; 13 (right)<BR><BR>March 2000 vs. February 2001<BR>becomes<BR>abs(3-2) + 12 * abs(2000-2001) --&#062;&#062; 13 (plain silly!)<BR><BR>OF COURSE it doesn&#039;t work! You aren&#039;t ensuring that the dates are ordered properly, so you are trying to rely on the abs function call. Nope. Never.<BR><BR>Try again.<BR><BR>The easy way to do this:<BR><BR>monthTotal += abs( year1*12+month1 - year2*12+month2 );<BR><BR>I think. Untested. Off the top of my head.<BR><BR>Yeah, that works. Assumes that months are numbered starting at zero, though I&#039;m not sure that matters. [thinking more: it shouldn&#039;t matter.]<BR><BR><BR>

5. Senior Member
Join Date
Dec 1969
Posts
2,031

## RE: Date Logic trouble

Two dates. He means two dates. <BR>A particular point or period of time at which something happened or existed, or is expected to happen OR a statement of calendar time. <BR>You take one date, and then yet another one, and you have 2.<BR>;)

6. Senior Member
Join Date
Dec 1969
Posts
96,118

## Kristin: You forgot...

...to explain what "difference" means. Remember, he needs the help.<BR><BR>&#060;snicker /&#062;<BR><BR>

7. Senior Member
Join Date
Dec 1969
Posts
2,031

## LOL. Ah Yes, my oversight ;)<eop>

.

8. Senior Member
Join Date
Dec 1969
Posts
11,334

## RE: illogic? Or ill logic?

Yep, that does work! I had the other function working until the point like you said... less than 12 months difference AND in a different calendar year.<BR><BR>Gawd, I miss VBS&#039; DateDiff :(<BR><BR>What did it in for me today is I fried my 4 remaining brain cells figuring out dereferencing, which still doesn&#039;t make sense to me...<BR><BR>Thanks Bill.

9. Senior Member
Join Date
Dec 1969
Posts
2,031

## Now...

don&#039;t you wish you posted an hour ago??? LOL. Now you can reply to my email with all the time Bill free&#039;d up for ya ;)

10. Senior Member
Join Date
Dec 1969
Posts
11,334

## Heh

Could&#039;ve been my explanation... I&#039;m really spent today :(<BR><BR>

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•