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
- First and foremost, open Word.
- Then click “Developer” tab if it’s activated already.
- 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.
- Now click “Normal” and then “Insert”.
- On the list menu, choose “Module”.
- Next, double click the new module to enter the editing area.
- 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
- Click “Run” to run the codes.
- Now you shall see a “Start Date” dialog box. Enter the start date in the text box and click “OK”.
- Next there is the “End Date” dialog box. Similarly, input the end date and click “OK”.
- Then a message box will pop up, indicating the difference between the 2 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
- First repeat the first 6 steps above.
- 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
- Remember to hit “Run”.
- In the “Start Time” box, enter the start time, such as “18:00:00”.
- 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.
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.
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