Skip to main content

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

Author: Xiaoyang Last Modified: 2025-05-12

通常,我們可以快速輕鬆地在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 功能AI助手 / 即時助手 / 超級潤色(保留格式)/ 超級翻譯(保留格式)/ AI遮擋 / AI校正...

📘 文件精通拆分頁面 / 合併文檔 / 以多種格式導出選擇內容(PDF/TXT/DOC/HTML...)/ 批量轉換為 PDF...

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

🧹 輕鬆清理:清除多餘空格 / 分節符 / 文本框 / 超鏈接 / 更多清除工具,請前往“清除”組...

創意插入:插入千位分隔符 / 複選框 / 選項按鈕 / 二維碼 / 條形碼 / 多張圖片 / 在“插入”組中發現更多...

🔍 精確選擇:精準定位特定頁面 / 表格 / 形狀 / 標題段落 / 使用更多選擇 ”功能增強導航...

星級增強功能跳轉至任意位置 / 自動插入重複文本 / 在文檔窗口之間切換 / 11 轉換工具...

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

最佳辦公效率工具

Kutools for Word - 100+ Word 工具