Date Diff Function

 

 

The DateDiff function returns the number of intervals between two dates.

Syntax
DateDiff(interval,date1,date2[,firstdayofweek[,firstweekofyear]]) 

Parameter Description 
interval Required. The interval you want to use to calculate the differences between date1 and date2
Can take the following values:

yyyy - Year 
q - Quarter 
m - Month 
y - Day of year 
d - Day 
w - Weekday 
ww - Week of year 
h - Hour 
n - Minute 
s - Second 
 
date1,date2 Required. Date expressions. Two dates you want to use in the calculation 
firstdayofweek Optional. Specifies the day of the week.
Can take the following values:

0 = vbUseSystemDayOfWeek - Use National Language Support (NLS) API setting 
1 = vbSunday - Sunday (default) 
2 = vbMonday - Monday 
3 = vbTuesday - Tuesday 
4 = vbWednesday - Wednesday 
5 = vbThursday - Thursday 
6 = vbFriday - Friday 
7 = vbSaturday - Saturday 
This VB tip will show you who to use the date diff function to see how many days are between the current date and a specified date. You could use this to see if an invoice is past due and then code your program to warn you that the invoice is past due.

This code will check and see if the current date and the comparison date is over 30 days. If the date is over 30 days then the date will be displayed in red.

Start a new project and put two text boxes on the form. Then cut and paste the code below.

************************************************************
Private Sub Text1_LostFocus()
Dim curDate, pstDate As Date
Dim difDate
curDate = Format(Now, "mm/dd/yy")
pstDate = DateValue(Text1)
difDate = DateDiff("d", pstDate, curDate)
If difDate <= 30 Then
Text2 = pstDate
Else
Text2 = pstDate
Text2.FontBold = True
Text2.ForeColor = QBColor(4)
End If
End Sub
*************************************************************

Now enter a date in the first text box and tab off the text box. If the date in the text1 is older then 30 days then the date will be displayed in red in text2, if not then it will just display the date in black.