Pages

Wednesday, June 20, 2012

Let DateDiff() determine if two dates are in the same month (VBScript 2.0)

To determine if two dates are in the same month, your first instinct may be to simply use the Month() function on each date, then compare the two resulting numbers. However, under these circumstances, the comparison would equate 1/1/2000 with 1/1/1999. Instead, the DateDiff() function provides one quick way to make this determination, like so:

DateDiff("m", Date1, Date2)

In this expression, the DateDiff() function finds the difference in calendar months between the two dates. If the expression returns a zero, then the two dates are in the same calendar month. To include this feature in a conditional expression, you could use the following in a query:

If DateDiff("m", Date1, Date2) Then
   'Month's are different
Else
   'Month's are same
End If



No comments:

Post a Comment

Note: Only a member of this blog may post a comment.