Skip to main content

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

Author: Xiaoyang Last Modified: 2025-08-06

通常,我們可以快速輕鬆地在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 -讓你的 Word進階,擁有超過 100 項強大功能!

🤖 Kutools AI FeaturesAI Assistant / 即時助手 / 超級潤色(保留格式) / 超級翻譯(保留格式) / AI遮擋 / AI校正...

📘 文檔掌控文檔拆分頁面 / 文檔合併 / 選取內容多格式導出(PDF/TXT/DOC/HTML...)/ 批量轉換為 PDF...

內容編輯跨多個文件批量查找與替換 / 圖片大小統一調整 / 行列翻轉表格 / 表格轉文本...

🧹 潔淨無憂:一鍵清除多餘空格 / 分節符 /文字方塊 / 超鏈接 / 更多清除工具請至 清除所有 群組...

創新插入:插入千位分隔符 / 複選框 / 選項按鈕 / 二維碼 / 條形碼 / 批量插入圖片 / 更多插入功能請前往 插入 群組...

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

優質強化輕鬆跳轉至文件夾 / 自動插入重複文字 / 切換文檔窗口 /11 轉換工具...

🌍 支援40+ 種語言:可用您的偏好語言體驗 Kutools —— 支援英語、西班牙語、德語、法語、中文等超過40 種語言!

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

最佳辦公效率工具

Kutools for Word - 100+ Word 工具