How to Calculate the Number of Days or Hours Between Two Dates or Times in a Word Document?

Normally, we can quickly and easily calculate the number of days between two dates in an Excel spreadsheet. However, have you ever tried calculating the number of days between two specific dates in a Word document?

Calculating the difference between two dates in a Word document using VBA

To calculate the number of days between two specific dates, the VBA code below can assist you. Please follow these steps:

  1. Press ALT + F11 to open the Microsoft Visual Basic for Applications window.
  2. Click Insert > Module, copy and paste the code below into the empty module:

Sub CalculateDateDifference()
Dim xStartDate As Date
Dim xEndDate As Date
Dim xDay As Long
On Error Resume Next
xStartDate = InputBox(“Enter the start date”, “Word Document”, “”)
xEndDate = InputBox(“Enter the end date”, “Word Document”, “”)
If (InStr(1, Str(xStartDate), “:”) > 0) Or (InStr(1, Str(xEndDate), “:”) > 0) Then
MsgBox “Please input valid dates”, vbInformation, “Word Document”
Exit Sub
End If
xDay = DateDiff(“d”, xStartDate, xEndDate)
MsgBox “There are ” & xDay & ” days between ” & xStartDate & ” and ” & xEndDate & vbCrLf, vbInformation, “Word Document”
End Sub

  1. Press F5 to run this code. In the consecutive dialog boxes, enter the start and end dates you want to use, as shown in the screenshot:
  1. Click OK, and you will receive the desired result, as shown in the screenshot:

Calculating the difference between two times in a Word document using VBA

Here is another VBA code that can help you calculate the difference between two specific times. Please follow these steps:

  1. Press ALT + F11 to open the Microsoft Visual Basic for Applications window.
  2. Click Insert > Module, copy and paste the code below into the empty module:

Sub CalculateTimeDifference()
Dim xStartTime As Date
Dim xEndTime As Date
Dim xTimeDiff As Long
Dim xHours As Long
On Error Resume Next
xStartTime = InputBox(“Enter the start time”, “Word Document”, “”)
xEndTime = InputBox(“Enter the end time”, “Word Document”, “”)
If (Str(xStartTime) = ” 0:00:00″) Or (Str(xEndTime) = ” 0:00:00″) _
Or (Str(xStartTime) = ” 12:00:00 AM”) Or (Str(xEndTime) = ” 12:00:00 AM”) Then
MsgBox “Please input valid times”, vbInformation, “Word Document”
Exit Sub
ElseIf xStartTime > xEndTime Then
MsgBox “The start time is not earlier than the end time!”, vbInformation, “Word Document”
Exit Sub
End If
xTimeDiff = DateDiff(“s”, xStartTime, xEndTime)
xHours = xTimeDiff \ 3600
xTimeDiff = xTimeDiff – xHours * 3600
MsgBox “There are ” & xHours & ” hours ” & xTimeDiff \ 60 & ” minutes ” & xTimeDiff – (xTimeDiff \ 60) * 60 _
& ” seconds between ” & xStartTime & ” and ” & xEndTime & vbCrLf, vbInformation, “Word Document”
End Sub

  1. Press F5 to run this code. In the consecutive dialog boxes, enter the start and end times you want to use to calculate the difference, as shown in the screenshot:
  1. Click OK, and the time difference between the two specified times will be calculated and displayed, as shown in the screenshot:
How to Calculate the Number of Days or Hours Between Two Dates or Times in a Word Document?

How to Calculate the Number of Days or Hours Between Two Dates or Times in a Word Document?

Gotkey.net
Logo
Compare items
  • Total (0)
Compare
0
Shopping cart