跳至主要内容

如何在Word文档中计算两个日期或时间之间的天数或小时数?

Author: Xiaoyang Last Modified: 2025-05-07

通常,我们可以快速轻松地在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 工具