Kutools for Excel 22.00 HOT

300+ Powerful Features You Must Have in Excel

Kutools-for-Excel

Kutools for Excel is a powerful add-in that frees you from performing time-consuming operations in Excel, such as combine sheets quickly, merge cells without losing data, paste to only visible cells, count cells by color and so on. 300+ powerful features / functions for Excel 2019, 2016, 2013, 2010, 2007 or Office 365!

Read More Download Buy now

Office Tab 14.00HOT

Adding Tabbed Interface for Office

Office Tab

It enables tabbed browsing, editing, and managing of Microsoft Office applications. You can open multiple documents / files in a single tabbed window, such as using the browser IE 8/9/10, Firefox, and Google Chrome. It's compatible with Office 2019, 2016, 2013, 2010, 2007, 2003 or Office 365. Demo

Read More Download Buy now

Kutools for Outlook 13.00NEW

100+ Powerful Features for Outlook

Kutools-for-Outlook

Kutools for Outlook is a powerful add-in that frees you from time-consuming operations which majority of Outlook users has to perform daily! It can save your time from using Microsoft Outlook 2019, 2016, 2013, 2010 or Office 365!

Read More Download Buy now

Kutools for Word  9.00NEW

100+ Powerful Features for Word

Kutools-for-Word

Kutools for Word is a powerful add-in that frees you from time-consuming operations which majority of Word users have to perform daily! It can save your time from using Microsoft Word / Office 2019, 2016, 2013, 2010, 2007, 2003 or Office 365!

Read More Download Buy now

Classic Menu for Office

Bringing Back Your Familiar Menus

Restores the old look and menus of Office 2003 to Microsoft Office 2019, 2016, 2013, 2010, 2007 or Office 365. Don’t lose time in finding commands on the new Ribbon. Easy to deploy to all computers in enterprises and organizations.

Read More Download Buy now

How to calculate days or hours between two dates or times In Word document?

Normally, we can calculate the number of days between two dates in Excel worksheet quickly and easily, but, have you ever tried to get the number of days between two given dates in a Word document?

Calculate difference between two dates in Word document with VBA code

Calculate difference between two times in Word document with VBA code


Calculate difference between two dates in Word document with VBA code

To calculate the number of days between two given dates, the below VBA code can do you a favor, please do as this:

1. Hold down the ALT + F11 keys to open the Microsoft Visual Basic for Applications window.

2. And then, click Insert > Module, copy and paste below code into the opened blank module:

VBA code: Calculate difference between two dates

Sub CalculateDateDifference()
    Dim xStartDate As Date
    Dim xEndDate As Date
    Dim xDay As Long
    On Error Resume Next
    xStartDate = InputBox("Enter the start date", "KuTools for Word", "")
    xEndDate = InputBox("Enter the end date", "KuTools for Word", "")
   If (InStr(1, Str(xStartDate), ":") > 0) Or (InStr(1, Str(xEndDate), ":") > 0) Then
        MsgBox "please input current date", vbInformation, "KuTools for Excel"
        Exit Sub
    End If
    xDay = DateDiff("d", xStartDate, xEndDate)
    MsgBox "There are " & xDay & " days left from " & xStartDate & " to " & xEndDate & vbCrLf, vbInformation, "KuTools for Word"
End Sub

3. And then, press F5 key to run this code, in the consecutive dialog boxes, enter the start date and end date that you want to use, see screenshot:

doc calculate days 1

4. Then, click OK button, and you will get the result that you want, see screenshot:

doc calculate days 2


Calculate difference between two times in Word document with VBA code

Here is another VBA code which can help you to calculate the difference between two given times, please do as this:

1. Hold down the ALT + F11 keys to open the Microsoft Visual Basic for Applications window.

2. And then, click Insert > Module, copy and paste below code into the opened blank module:

VBA code: Calculate difference between two times

Sub CalculateTimeDifference()
    Dim xStartDate As Date
    Dim xEndDate As Date
    Dim xTime As Long
    Dim xHour As Long
    On Error Resume Next
    xStartDate = InputBox("Enter the start time", "KuTools for Word", "")
    xEndDate = InputBox("Enter the end time", "KuTools for Word", "")
    Debug.Print Str(xStartDate)
    If (Str(xStartDate) = " 0:00:00") Or (Str(xEndDate) = " 0:00:00") _
    Or (Str(xStartDate) = " 12:00:00 AM") Or (Str(xEndDate) = " 12:00:00 AM") Then
        MsgBox "please input the time", vbInformation, "KuTools for Excel"
        Exit Sub
    ElseIf xStartDate > xEndDate Then
        MsgBox " The start time is not larger than the end time!", vbInformation, "KuTools for Excel"
        Exit Sub
    End If
    xTime = DateDiff("s", xStartDate, xEndDate)
    xHour = xTime \ 3600
    xTime = xTime - xHour * 3600
    MsgBox "There are " & xHour & " hours " & xTime \ 60 & " minutes " & xTime - (xTime \ 60) * 60 _
            & " seconds left from " & xStartDate & " to " & xEndDate & vbCrLf, vbInformation, "KuTools for Word"
End Sub

3. And then, press F5 key to run this code, in the consecutive dialog boxes, enter the start time and end time that you want to use to calculate difference, see screenshot:

doc calculate days 3

4. Then, click OK button, and the time difference between two given times has been calculated and displayed as following screenshot shown:

doc calculate days 4



Recommended Word Productivity Tools

 

Kutools For Word - More Than 100 Advanced Features For Word, Save Your 50% Time

  • Complicated and repeated operations can be done one-time processing in seconds.
  • Insert multiple images across folders into Word document at once.
  • Merge and combine multiple Word files across folders into one with your desired order.
  • Split the current document into separate documents according to heading, section break or other criteria.
  • Convert files between Doc and Docx, Docx and PDF, collection of tools for common conversions and selection, and so on...
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.

Be the first to comment.

Feature Tutorials