2 Quick Ways to Get the Difference between Two Dates or Times via Word VBA

In this article, we want to present you 2 quick and efficient ways to get the difference between 2 specific dates or times via Word macros.

To calculate the days or time left between 2 concrete dates or times is of no difficulty. You can either do the math by yourself or get a date or time duration calculator. And here we want to demonstrate you the 2 macro ways which enable you to get the date or time difference in Word. Following are the detailed steps:

Get the Date Difference

  1. First and foremost, open Word.
  2. Then click “Developer” tab if it’s activated already.
  3. Next, click “Visual Basic” to open the VBA editor. You can refer to How to Insert Background Music into Your Word Document to add “Developer” to the Ribbon. Or you can also press “Alt+ F11” instead if you prefer not to.Click "Developer"->Click "Visual Basic"
  4. Now click “Normal” and then “Insert”.
  5. On the list menu, choose “Module”.Click "Normal"->Click "Insert"->Click "Module"
  6. Next, double click the new module to enter the editing area.
  7. Paste the following codes:
Sub CalculateDateDifference()
  Dim dtStartDate As Date
  Dim dtEndDate As Date
  Dim lDaysLeft As Long

  '  Input the start date and end date, calculate the date difference.
  dtStartDate = InputBox("Enter the start date","Start Date","For example:2017/1/1")
  dtEndDate = InputBox("Enter the end date","End Date","For example:2017/2/1")
  lDaysLeft = DateDiff("d", dtStartDate, dtEndDate)

  '  Output the calculate the date difference between the two dates. 
  MsgBox ("There are " & lDaysLeft & " days left from " & dtStartDate & " to " & dtEndDate & vbCrLf)
End Sub
  1. Click “Run” to run the codes.Paste Codes->Click "Run"
  2. Now you shall see a “Start Date” dialog box. Enter the start date in the text box and click “OK”.Enter Start Date->Click "OK"
  3. Next there is the “End Date” dialog box. Similarly, input the end date and click “OK”.Enter End Date->Click "OK"
  4. Then a message box will pop up, indicating the difference between the 2 dates.Message Box Showing the Difference between Dates

In case you need to insert the result into your document, you can edit the macro a bit by change the code line “MsgBox (“There are ” & lDaysLeft & ” days left from ” & dtStartDate & ” to ” & dtEndDate & vbCrLf)” to “Selection.Text = “There are ” & lDaysLeft & ” days left from ” & dtStartDate & ” to ” & dtEndDate & vbCrLf”.

Get the Time Difference

  1. First repeat the first 6 steps above.
  2. Then paste these codes instead:
Sub CalculateTimeDifference()
  Dim dtStartTime As Date
  Dim dtEndTime As Date
  Dim lTimeLeft As Long
  Dim lHour As Long
  Dim lMinute As Long
  Dim lSecond As Long
  '  Get the start time and the end time.
  dtStartTime = InputBox("Enter the start time","Start Time", "For example:18:00:00") 
  dtEndTime = InputBox("Enter the end time","End Time", "For example:18:00:00") 
  '  Calculate the time difference. 
  lTimeLeft = DateDiff("s", dtStartTime, dtEndTime)
  lHour = lTimeLeft \ 3600
  lTimeLeft = lTimeLeft - lHour * 3600
  lMinute = lTimeLeft \ 60
  lSecond = lTimeLeft - lMinute * 60
  MsgBox ("There are " & lHour & " hours " & lMinute & " minutes " & lSecond & " seconds left from " & dtStartTime & " to " & dtEndTime & vbCrLf)
End Sub
  1. Remember to hit “Run”.Paste Codes->Hit "Run"
  2. In the “Start Time” box, enter the start time, such as “18:00:00”.Enter Start Time->Click "OK"
  3. Then type the end time in “End Time” dialog box and click “OK”. And you can check the outcome in the message box coming up.Type End Time->Click "OK"The Time Difference

Mitigate File Loss Risk

Now and then, Word can stop working out of the blue, leaving our files in great danger. Therefore, to reduce the chance of getting files damaged, we suggest you making regular backups. Also, get a Word file recovery product is necessary when you find yourself in the need of fixing broken files.

Author Introduction:

Vera Chen is a data recovery expert in DataNumen, Inc., which is the world leader in data recovery technologies, including Excel xls data repair tool and pdf repair software products. For more information visit www.datanumen.com

Comments are closed.