VBA DateDiff function in Excel is categorized as a Date & Time function. This is a built-in Excel VBA Function. This function returns the difference between two date values, based on the interval specified. We can use this function in VBA and can’t use in Excel.

This function we use in either procedure or function in a VBA editor window in Excel. We can use this VBA DateDiff Function in any number of times in any number of procedures or functions. Let us learn what is the syntax and parameters of the DateDiff function, where we can use this DateDiff Function and real-time examples in Excel VBA.

**Table of Contents:**

- Objective
- Syntax of VBA DateDiff Function
- Parameters or Arguments
- Where we can apply or use VBA DateDiff Function?
- Example 1: Compute the number of
**days** - Example 2: Compute the number of
**months** - Example 3: Compute the number of
**years** - Example 4: Compute the number of
**weeks** - Example 5: Compute the number of
**hours** - Instructions to Run VBA Macro Code
- Other Useful Resources

## Syntax of VBA DateDiff Function

The syntax of the DateDiff Function in VBA is

DateDiff(Interval,Date1,Date2,[FirstDayOfWeek],[FirstWeekOfYear])

The DateDiff function returns a string value.

#### Parameters or Arguments:

The DateDiff function has five arguments in Excel VBA.

where

**Interval:**The Interval is a required argument. It represents the interval of time to calculate the difference between two Date1 and Date2. Here is a list of interval values.

Interval | Description |
---|---|

yyyy | Year |

q | Quarter |

m | Month |

y | Day of year |

d | Day |

w | Weekday |

ww | Week |

h | Hour |

n | Minute |

s | Second |

**Date1:**The Date1 is a required argument. It represents date.

**Date2:**The Date2 is a required argument. It represents date.

**[FirstDayOfWeek]:**The [FirstDayOfWeek] is an optional constant argument. It represents the first day of the week. If this argument is ignored, Sunday(vbSunday) is the default value.

VB Constant | Value | Description |
---|---|---|

vbUseSystem | 0 | Use the National Language Support(NLS) API setting |

vbSunday | 1 | Sunday (default) |

vbMonday | 2 | Monday |

vbTuesday | 3 | Tuesday |

vbWednesday | 4 | Wednesday |

vbThursday | 5 | Thursday |

vbFriday | 6 | Friday |

vbSaturday | 7 | Saturday |

**[FirstWeekOfYear]:**The [FirstWeekOfYear] is an optional constant argument. It represents the first week of the year. If this argument is ignored, 1st Jan(vbFirstJan1) is the default value.

VB Constant | Value | Description |
---|---|---|

vbUseSystem | 0 | Use the National Language Support(NLS) API setting |

vbFirstJan1 | 1 | The week in which January 1 occurs (default) |

vbFirstFourDays | 2 | The first week consists at least four days in the year |

vbFirstFullWeek | 3 | The first full week of the year |

### Where we can apply or use VBA DateDiff Function?

We can use this DateDiff Function in VBA MS Office 365, MS Excel 2016, MS Excel 2013, 2011, Excel 2010, Excel 2007, Excel 2003, Excel 2016 for Mac, Excel 2011 for Mac, Excel Online, Excel for iPhone, Excel for iPad, Excel for Android tablets and Excel for Android Mobiles.

### Example 1: Compute the number of days between a start date and end date

Here is a simple example of the VBA DateDiff function. This below example computes the number of days between a start date and end date.

'Compute the number of days between a start date and end date Sub VBA_DateDiff_Function_Ex1() 'Variable declaration Dim sDate As Date Dim eDate As Date Dim iDate As Integer sDate = "02/04/2018" eDate = "04/06/2018" iDate = DateDiff("d", sDate, eDate) MsgBox "The number of days between " & sDate & " and " & eDate & " : " & iDate, vbInformation, "VBA DateDiff Function" End Sub

**Output:** Here is the screen shot of the first example output.

### Example 2: Compute the number of months between a start date and end date

Let us see one more example of the VBA DateDiff function. This below example computes the number of months between a start date and end date.

'Compute the number of months between a start date and end date Sub VBA_DateDiff_Function_Ex2() 'Variable declaration Dim sDate As Date Dim eDate As Date Dim iDate As Integer sDate = "02/04/2018" eDate = "04/06/2018" iDate = DateDiff("m", sDate, eDate) MsgBox "The number of months between " & sDate & " and " & eDate & " : " & iDate, vbInformation, "VBA DateDiff Function" End Sub

**Output:** Here is the screen shot of the second example output.

### Example 3: Compute the number of years between a start date and end date

Let us see another example of the VBA DateDiff function. This below example computes the number of years between a start date and end date.

'Compute the number of years between a start date and end date Sub VBA_DateDiff_Function_Ex3() 'Variable declaration Dim sDate As Date Dim eDate As Date Dim iDate As Integer sDate = "02/04/2018" eDate = "04/06/2025" iDate = DateDiff("yyyy", sDate, eDate) MsgBox "The number of years between " & sDate & " and " & eDate & " : " & iDate, vbInformation, "VBA DateDiff Function" End Sub

**Output:** Here is the screen shot of the third example output.

### Example 4: Compute the number of weeks between a start date and end date

One more example of the VBA DateDiff function. This below example computes the number of weeks between a start date and end date

'Compute the number of weeks between a start date and end date Sub VBA_DateDiff_Function_Ex4() 'Variable declaration Dim sDate As Date Dim eDate As Date Dim iDate As Integer sDate = "02/04/2018" eDate = "04/06/2018" iDate = DateDiff("w", sDate, eDate) MsgBox "The number of weeks between " & sDate & " and " & eDate & " : " & iDate, vbInformation, "VBA DateDiff Function" End Sub

**Output:** Here is the screen shot of the fourth example output.

### Example 5: Compute the number of hours between a start time and end time

One more example of the VBA DateDiff function. This below example computes the number of hours between a start time and end time.

'Compute the number of hours between a start time and end time Sub VBA_DateDiff_Function_Ex5() 'Variable declaration Dim sTime As Date Dim eTime As Date Dim iTime As Integer sTime = "02:04:10" eTime = "08:06:35" iTime = DateDiff("h", sTime, eTime) MsgBox "The number of hours between " & sTime & " and " & eTime & " : " & iTime, vbInformation, "VBA DateDiff Function" End Sub

**Output:** Here is the screen shot of the fifth example output.

#### Instructions to Run VBA Macro Code or Procedure:

You can refer the following link for the step by step instructions.

Instructions to run VBA Macro Code

#### Other Useful Resources:

Click on the following links of the useful resources. These helps to learn and gain more knowledge.

VBA Tutorial VBA Functions List VBA Arrays in Excel Blog

VBA Editor Keyboard Shortcut Keys List VBA Interview Questions & Answers