Skip to main content

Kutools for Office — 一套工具,五種功能。完成更多工作。

如何在Word文檔中計算兩個日期或時間之間的天數或小時數?

Author Xiaoyang Last modified

通常,我們可以快速輕鬆地在Excel工作表中計算兩個日期之間的天數,但是,您是否嘗試過在Word文檔中獲取兩個指定日期之間的天數呢?

使用VBA代碼計算Word文檔中兩個日期之間的差異

使用VBA代碼計算Word文檔中兩個時間之間的差異


使用VBA代碼計算Word文檔中兩個日期之間的差異

要計算兩個指定日期之間的天數,以下VBA代碼可以幫助您,請按照以下步驟操作:

1. 按住 ALT + F11鍵打開Microsoft Visual Basic for Applications窗口。

2. 然後,點擊 插入 > 模塊,將以下代碼複製並粘貼到打開的空白模塊中:

VBA代碼:計算兩個日期之間的差異

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. 然後,按F5鍵運行此代碼,在連續出現的對話框中,輸入您想使用的開始日期和結束日期,參見截圖:

Enter the start and end date

4. 然後,點擊 確定按鈕,您將獲得想要的結果,參見截圖:

Pop-up displaying days between the two dates


使用VBA代碼計算Word文檔中兩個時間之間的差異

這裡有另一個VBA代碼,可以幫助您計算兩個指定時間之間的差異,請按照以下步驟操作:

1. 按住ALT + F11鍵打開Microsoft Visual Basic for Applications窗口。

2. 然後,點擊 插入 > 模塊,將以下代碼複製並粘貼到打開的空白模塊中:

VBA代碼:計算兩個時間之間的差異

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. 然後,按F5鍵運行此代碼,在連續出現的對話框中,輸入您想用來計算差異的開始時間和結束時間,參見截圖:

Enter the start and end time

4. 然後,點擊確定按鈕,兩個指定時間之間的時間差已經被計算並顯示如下截圖所示:

Pop-up displaying time difference between the two times

最佳辦公效率工具

Kutools for Word -透過超過 100 項卓越功能,提升您的 Word 使用體驗!

🤖 Kutools AI FeaturesAI AssistantReal-Time AssistantSuper Polish(保留格式)Super Translate(保留格式)AI RedactionAI Proofread...

📘 文件掌握分割頁面文檔合併區域導出成各種格式(PDF/TXT/DOC/HTML...)批量轉換為 PDF...

正文編輯批量查找與替換多個文件批量調整所有圖片大小翻轉表格的行與列表格轉文本...

🧹 輕鬆清理:快速清除多餘空格分節符文字框超連結/更多清除工具請前往刪除工具組...

創意插入:插入千位分隔符複選框選項按鈕二維碼條形碼批量插入圖片/還有更多精彩功能在插入工具組...

🔍 精確選擇:精確定位特定頁面表格形狀標題段落/使用更多 選擇 功能提升導航效率...

亮點增強導航至任何區域自動插入重複文字切換文檔窗口11 轉換工具...

🌍 支援40+種語言:可用 Kutools於您偏好語言環境——支援英語、西班牙語、德語、法語、中文以及超過40種語言!

Kutools and Kutools Plus tabs on the Word Ribbon
👉 想立即體驗這些功能嗎?立即下載 Kutools for Word!🚀
 

✨ Kutools for Office——一次安裝,五大強力工具!

包含 Office Tab Pro · Kutools for Excel · Kutools for Outlook · Kutools for Word · Kutools for PowerPoint

📦 五大工具一次全擁有 | 🔗 與 Microsoft Office 無縫整合 | ⚡ 即刻省時提高工作效率

最佳辦公效率工具

Kutools for Word - 100+ 款 Word 工具